Reputation: 2292
Is there a way to test if a connection string is valid without attempting to open a connection?
I need to find out if a connection string is a MySql connection string, or a SQL Server connection string?
Edit
Here's what I've tried
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; }
Both mySqlBuilder
and msSqlBuilder
will take the connection string with no exception thrown.
Also, MySqlConnectionStringBuilder.ToString()
returns server=myServerAddress;database=myDataBase;user id=myUsername;password=myPassword
Upvotes: 3
Views: 1688
Reputation: 16423
It depends on the way you are connecting, if you specify a username and password in the connection string then there is a difference:
MS SQL Server connection string using username/password:
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
MySql connection string using username/password:
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
So you could check if the connection string contains Password
or Pwd
.
Upvotes: 1