mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-17 22:11:48 +08:00
42 lines
1.7 KiB
C#
42 lines
1.7 KiB
C#
using System;
|
|
using System.Data.SqlClient;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace mRemoteNG.Config.DatabaseConnectors
|
|
{
|
|
/// <summary>
|
|
/// A helper class for testing database connectivity
|
|
/// </summary>
|
|
public class DatabaseConnectionTester
|
|
{
|
|
public async Task<ConnectionTestResult> TestConnectivity(string type,
|
|
string server,
|
|
string database,
|
|
string username,
|
|
string password)
|
|
{
|
|
using (var dbConnector = DatabaseConnectorFactory.DatabaseConnector(type, server, database, username, password))
|
|
{
|
|
try
|
|
{
|
|
await dbConnector.ConnectAsync();
|
|
return ConnectionTestResult.ConnectionSucceded;
|
|
}
|
|
catch (SqlException sqlException)
|
|
{
|
|
if (sqlException.Message.Contains("The server was not found"))
|
|
return ConnectionTestResult.ServerNotAccessible;
|
|
if (sqlException.Message.Contains("Cannot open database"))
|
|
return ConnectionTestResult.UnknownDatabase;
|
|
if (sqlException.Message.Contains("Login failed for user"))
|
|
return ConnectionTestResult.CredentialsRejected;
|
|
return ConnectionTestResult.UnknownError;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return ConnectionTestResult.UnknownError;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |