mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-17 22:11:48 +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>
99 lines
2.8 KiB
C#
99 lines
2.8 KiB
C#
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Data.SqlClient;
|
|
using System.Threading.Tasks;
|
|
|
|
// ReSharper disable ArrangeAccessorOwnerBody
|
|
|
|
namespace mRemoteNG.Config.DatabaseConnectors
|
|
{
|
|
public class MSSqlDatabaseConnector : IDatabaseConnector
|
|
{
|
|
private DbConnection _dbConnection { get; set; } = default(SqlConnection);
|
|
private string _dbConnectionString = "";
|
|
private readonly string _dbHost;
|
|
private readonly string _dbCatalog;
|
|
private readonly string _dbUsername;
|
|
private readonly string _dbPassword;
|
|
|
|
public DbConnection DbConnection()
|
|
{
|
|
return _dbConnection;
|
|
}
|
|
|
|
public DbCommand DbCommand(string dbCommand)
|
|
{
|
|
return new SqlCommand(dbCommand, (SqlConnection) _dbConnection);
|
|
}
|
|
|
|
public bool IsConnected
|
|
{
|
|
get { return (_dbConnection.State == ConnectionState.Open); }
|
|
}
|
|
|
|
public MSSqlDatabaseConnector(string sqlServer, string catalog, string username, string password)
|
|
{
|
|
_dbHost = sqlServer;
|
|
_dbCatalog = catalog;
|
|
_dbUsername = username;
|
|
_dbPassword = password;
|
|
Initialize();
|
|
}
|
|
|
|
private void Initialize()
|
|
{
|
|
BuildSqlConnectionString();
|
|
_dbConnection = new SqlConnection(_dbConnectionString);
|
|
}
|
|
|
|
private void BuildSqlConnectionString()
|
|
{
|
|
if (_dbUsername != "")
|
|
BuildDbConnectionStringWithCustomCredentials();
|
|
else
|
|
BuildDbConnectionStringWithDefaultCredentials();
|
|
}
|
|
|
|
private void BuildDbConnectionStringWithCustomCredentials()
|
|
{
|
|
_dbConnectionString = $"Data Source={_dbHost};Initial Catalog={_dbCatalog};User Id={_dbUsername};Password={_dbPassword}";
|
|
}
|
|
|
|
private void BuildDbConnectionStringWithDefaultCredentials()
|
|
{
|
|
_dbConnectionString = $"Data Source={_dbHost};Initial Catalog={_dbCatalog};Integrated Security=True";
|
|
}
|
|
|
|
public void Connect()
|
|
{
|
|
_dbConnection.Open();
|
|
}
|
|
|
|
public async Task ConnectAsync()
|
|
{
|
|
await _dbConnection.OpenAsync();
|
|
}
|
|
|
|
public void Disconnect()
|
|
{
|
|
_dbConnection.Close();
|
|
}
|
|
|
|
public void AssociateItemToThisConnector(DbCommand dbCommand)
|
|
{
|
|
dbCommand.Connection = (SqlConnection) _dbConnection;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Dispose(true);
|
|
}
|
|
|
|
private void Dispose(bool itIsSafeToFreeManagedObjects)
|
|
{
|
|
if (!itIsSafeToFreeManagedObjects) return;
|
|
_dbConnection.Close();
|
|
_dbConnection.Dispose();
|
|
}
|
|
}
|
|
} |