Files
mRemoteNG/mRemoteNG/Config/DatabaseConnectors/DatabaseConnectionTester.cs
Dimitrij 83f3846ce6 lib updates
correct build number calculations - now its days from last release + hour + minute of build
some changes to migrate to json schema + preparation of using db to save settings
2024-05-03 14:40:52 +01:00

44 lines
1.8 KiB
C#

using System;
using System.Data.SqlClient;
using System.Runtime.Versioning;
using System.Threading.Tasks;
namespace mRemoteNG.Config.DatabaseConnectors
{
[SupportedOSPlatform("windows")]
/// <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 (IDatabaseConnector 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;
}
}
}
}
}