2

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??

4

2 回答 2

2

您可以像这样定义一个通用方法:

static bool TestConnectionString<T>(string connectionString) where T : DbConnection, new()
{
    using (DbConnection connection = new T())
    {
        connection.ConnectionString = connectionString;

        try
        {
            connection.Open();
            return true;
        }
        catch (SqlException)
        {
            return false;
        }
    }
}
于 2017-05-09T08:14:12.373 回答
0

您应该关闭连接。否则,您将继续打开连接并使其保持打开状态。类似于 Alessandro 的答案,但代码中包含 close :

static bool TestConnectionString<T>(string connectionString) where T : DbConnection, new()
{
    using (DbConnection connection = new T())
    {
        connection.ConnectionString = connectionString;

        try
        {
            connection.Open();
            connection.Close();
            return true;
        }
        catch (SqlException)
        {
            return false;
        }
    }
}
于 2018-04-16T16:35:10.493 回答