I am having different data source like mysql, SQL Server and Oracle and for each of the data source i will test the whether handshake with the server is successful or not based on the connection string.
So I have created 3 class and each of this class will have a method to test connection string but the code is same so I was thinking that whether it is possible to to create 1 generic method that will handle all the 3 data source like mysql, SQL Server and Oracle so that I don't have to create 3 method for each of the data source to test connection string.
Below is my code :
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;
}
}
}
}
So any better way to do this above process??