2
私はmysql、sql server、oracleのような異なるデータソースを持っていますが、データソースごとに、サーバーとのハンドシェイクが成功したかどうかを接続文字列。さまざまなrdbmsの接続文字列をテストする一般的な方法
私は3クラスを作成しました。このクラスはそれぞれ、接続文字列をテストするメソッドを持ちますが、コードは同じですので、3つのデータすべてを処理する汎用メソッドを1つ作成できるかどうかmysql、sql server、oracleのようなソースを使用して、データソースごとに3つのメソッドを作成して接続文字列をテストする必要はありません。以下は
は私のコードです:だから
public class ConnectionViewModel
{
public string RdbmsType { get; set; }
public string ConnectionString { get; set; }
}
[HttpPost]
public ActionResult RdbmsServerHandshake(ConnectionViewModel model)
{
if (model.RdbmsType =="Mysql")
{
var mySqlRepo = new MysqlRepository();
var test = mySqlRepo.TestConnectionString(model.ConnectionString);
}
else if(model.RdbmsType == "SqlServer")
{
var sqlServerRepo = new SqlServerRepository();
var test = sqlServerRepo.TestConnectionString(model.ConnectionString);
}
else // for oracle
{
// code for oracle
}
}
public class SqlServerRepository
{
public bool TestConnectionString(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}
}
}
public class MysqlRepository
{
public bool TestConnectionString(string connectionString)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}
}
}
この上記のプロセスを行うための任意のより良い方法?
私は、任意の助けを感謝します:)
を:) –