mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-26 12:08:37 +08:00
* Uses MySQL Connector/NET from nuget * Adds SQL Server type to configuration. * Hostname for MySQL connections can include port - Format: <hostname>[:<port>] * Abstracted a bundle of stuff to be generic for both MSSQL and MySQL, including a number of variable and method names. (Mostly went from "sql..." -> "db..." * Changed MiscTools.DBDate() string building for MSSQL, uses DateTime.ToString() with a format which seemed simpler. * Unsure about which lines in .csproj are actually required, and which are auto-munged by Visual Studio. * ... This is my first C# (and VS!) work, be gentle! Signed-off-by: Mike Beattie <mike@ethernal.org>
177 lines
7.2 KiB
C#
177 lines
7.2 KiB
C#
using System;
|
|
using mRemoteNG.App;
|
|
using mRemoteNG.Config.Connections;
|
|
using mRemoteNG.Config.Connections.Multiuser;
|
|
using mRemoteNG.Config.DatabaseConnectors;
|
|
using mRemoteNG.Security.SymmetricEncryption;
|
|
|
|
namespace mRemoteNG.UI.Forms.OptionsPages
|
|
{
|
|
public sealed partial class SqlServerPage
|
|
{
|
|
private readonly DatabaseConnectionTester _databaseConnectionTester;
|
|
|
|
public SqlServerPage()
|
|
{
|
|
InitializeComponent();
|
|
ApplyTheme();
|
|
PageIcon = Resources.Database_Icon;
|
|
_databaseConnectionTester = new DatabaseConnectionTester();
|
|
}
|
|
|
|
public override string PageName
|
|
{
|
|
get => Language.strSQLServer.TrimEnd(':');
|
|
set { }
|
|
}
|
|
|
|
public override void ApplyLanguage()
|
|
{
|
|
base.ApplyLanguage();
|
|
|
|
lblExperimental.Text = Language.strExperimental.ToUpper();
|
|
lblSQLInfo.Text = Language.strSQLInfo;
|
|
|
|
chkUseSQLServer.Text = Language.strUseSQLServer;
|
|
lblSQLServer.Text = Language.strLabelHostname;
|
|
lblSQLDatabaseName.Text = Language.strLabelSQLServerDatabaseName;
|
|
lblSQLUsername.Text = Language.strLabelUsername;
|
|
lblSQLPassword.Text = Language.strLabelPassword;
|
|
lblSQLReadOnly.Text = Language.strLabelReadOnly;
|
|
btnTestConnection.Text = Language.TestConnection;
|
|
}
|
|
|
|
public override void LoadSettings()
|
|
{
|
|
chkUseSQLServer.Checked = Settings.Default.UseSQLServer;
|
|
txtSQLType.Text = Settings.Default.SQLServerType;
|
|
txtSQLServer.Text = Settings.Default.SQLHost;
|
|
txtSQLDatabaseName.Text = Settings.Default.SQLDatabaseName;
|
|
txtSQLUsername.Text = Settings.Default.SQLUser;
|
|
var cryptographyProvider = new LegacyRijndaelCryptographyProvider();
|
|
txtSQLPassword.Text = cryptographyProvider.Decrypt(Settings.Default.SQLPass, Runtime.EncryptionKey);
|
|
chkSQLReadOnly.Checked = Settings.Default.SQLReadOnly;
|
|
lblTestConnectionResults.Text = "";
|
|
}
|
|
|
|
public override void SaveSettings()
|
|
{
|
|
base.SaveSettings();
|
|
var sqlServerWasPreviouslyEnabled = Settings.Default.UseSQLServer;
|
|
|
|
Settings.Default.UseSQLServer = chkUseSQLServer.Checked;
|
|
Settings.Default.SQLServerType = txtSQLType.Text;
|
|
Settings.Default.SQLHost = txtSQLServer.Text;
|
|
Settings.Default.SQLDatabaseName = txtSQLDatabaseName.Text;
|
|
Settings.Default.SQLUser = txtSQLUsername.Text;
|
|
var cryptographyProvider = new LegacyRijndaelCryptographyProvider();
|
|
Settings.Default.SQLPass = cryptographyProvider.Encrypt(txtSQLPassword.Text, Runtime.EncryptionKey);
|
|
Settings.Default.SQLReadOnly = chkSQLReadOnly.Checked;
|
|
|
|
if (Settings.Default.UseSQLServer)
|
|
ReinitializeSqlUpdater();
|
|
else if (!Settings.Default.UseSQLServer && sqlServerWasPreviouslyEnabled)
|
|
DisableSql();
|
|
}
|
|
|
|
private static void ReinitializeSqlUpdater()
|
|
{
|
|
Runtime.ConnectionsService.RemoteConnectionsSyncronizer?.Dispose();
|
|
Runtime.ConnectionsService.RemoteConnectionsSyncronizer =
|
|
new RemoteConnectionsSyncronizer(new SqlConnectionsUpdateChecker());
|
|
Runtime.ConnectionsService.LoadConnections(true, false, "");
|
|
}
|
|
|
|
private void DisableSql()
|
|
{
|
|
Runtime.ConnectionsService.RemoteConnectionsSyncronizer?.Dispose();
|
|
Runtime.ConnectionsService.RemoteConnectionsSyncronizer = null;
|
|
Runtime.LoadConnections(true);
|
|
}
|
|
|
|
private void chkUseSQLServer_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
toggleSQLPageControls(chkUseSQLServer.Checked);
|
|
}
|
|
|
|
private void toggleSQLPageControls(bool useSQLServer)
|
|
{
|
|
lblSQLType.Enabled = useSQLServer;
|
|
lblSQLServer.Enabled = useSQLServer;
|
|
lblSQLDatabaseName.Enabled = useSQLServer;
|
|
lblSQLUsername.Enabled = useSQLServer;
|
|
lblSQLPassword.Enabled = useSQLServer;
|
|
lblSQLReadOnly.Enabled = useSQLServer;
|
|
txtSQLType.Enabled = useSQLServer;
|
|
txtSQLServer.Enabled = useSQLServer;
|
|
txtSQLDatabaseName.Enabled = useSQLServer;
|
|
txtSQLUsername.Enabled = useSQLServer;
|
|
txtSQLPassword.Enabled = useSQLServer;
|
|
chkSQLReadOnly.Enabled = useSQLServer;
|
|
btnTestConnection.Enabled = useSQLServer;
|
|
}
|
|
|
|
private async void btnTestConnection_Click(object sender, EventArgs e)
|
|
{
|
|
var type = txtSQLType.Text;
|
|
var server = txtSQLServer.Text;
|
|
var database = txtSQLDatabaseName.Text;
|
|
var username = txtSQLUsername.Text;
|
|
var password = txtSQLPassword.Text;
|
|
|
|
lblTestConnectionResults.Text = Language.TestingConnection;
|
|
imgConnectionStatus.Image = Resources.loading_spinner;
|
|
btnTestConnection.Enabled = false;
|
|
|
|
var connectionTestResult =
|
|
await _databaseConnectionTester.TestConnectivity(type, server, database, username, password);
|
|
|
|
btnTestConnection.Enabled = true;
|
|
|
|
switch (connectionTestResult)
|
|
{
|
|
case ConnectionTestResult.ConnectionSucceded:
|
|
UpdateConnectionImage(true);
|
|
lblTestConnectionResults.Text = Language.ConnectionSuccessful;
|
|
break;
|
|
case ConnectionTestResult.ServerNotAccessible:
|
|
UpdateConnectionImage(false);
|
|
lblTestConnectionResults.Text =
|
|
BuildTestFailedMessage(string.Format(Language.ServerNotAccessible, server));
|
|
break;
|
|
case ConnectionTestResult.CredentialsRejected:
|
|
UpdateConnectionImage(false);
|
|
lblTestConnectionResults.Text =
|
|
BuildTestFailedMessage(string.Format(Language.LoginFailedForUser, username));
|
|
break;
|
|
case ConnectionTestResult.UnknownDatabase:
|
|
UpdateConnectionImage(false);
|
|
lblTestConnectionResults.Text =
|
|
BuildTestFailedMessage(string.Format(Language.DatabaseNotAvailable, database));
|
|
break;
|
|
case ConnectionTestResult.UnknownError:
|
|
UpdateConnectionImage(false);
|
|
lblTestConnectionResults.Text = BuildTestFailedMessage(Language.strRdpErrorUnknown);
|
|
break;
|
|
default:
|
|
UpdateConnectionImage(false);
|
|
lblTestConnectionResults.Text = BuildTestFailedMessage(Language.strRdpErrorUnknown);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void UpdateConnectionImage(bool connectionSuccess)
|
|
{
|
|
imgConnectionStatus.Image = connectionSuccess
|
|
? Resources.tick
|
|
: Resources.exclamation;
|
|
}
|
|
|
|
private string BuildTestFailedMessage(string specificMessage)
|
|
{
|
|
return Language.strConnectionOpenFailed +
|
|
Environment.NewLine +
|
|
specificMessage;
|
|
}
|
|
}
|
|
} |