如何判断连接字符串是否属于MySql或SQL Server?

时间:2014-07-28 13:50:15

标签: c# mysql sql-server connection-string

有没有办法在不尝试打开连接的情况下测试连接字符串是否有效?

我需要查明连接字符串是MySql连接字符串还是SQL Server连接字符串?

修改

这是我尝试过的事情

var connectionString = "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"

MySqlConnectionStringBuilder mySqlBuilder;
try { mySqlBuilder = new MySqlConnectionStringBuilder(connectionString); }
catch { mySqlBuilder = null; }

SqlConnectionStringBuilder msSqlBuilder;
try { msSqlBuilder = new SqlConnectionStringBuilder(connectionString); }
catch { msSqlBuilder = null; }

mySqlBuildermsSqlBuilder都将获取连接字符串,不会抛出任何异常。

此外,MySqlConnectionStringBuilder.ToString()会返回server=myServerAddress;database=myDataBase;user id=myUsername;password=myPassword

1 个答案:

答案 0 :(得分:1)

这取决于您的连接方式,如果您在连接字符串中指定用户名和密码,则会有所不同:

使用用户名/密码的MS SQL Server连接字符串:

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

使用用户名/密码的MySql连接字符串:

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

因此,您可以检查连接字符串是否包含PasswordPwd