Correct way to get username and password from connection string?

I have a connection string, for example:

"SERVER=localhost;DATABASE=tree;UID=root;PASSWORD=branch;Min Pool Size = 0;Max Pool Size=200" 

How do I get various database parameters? I can get the database name and server as follows:

 serverName = conObject.DataSource; dbName = conObject.Database; 

I also need a username and password. The MySqlConnection object is not defined.

I am currently doing it like this:

 public static void GetDatabaseParameters(string connectionString, out string serverName, out string dbName, out string userName, out string password) { Match m = Regex.Match(connectionString, "SERVER=(.*?);DATABASE=(.*?);UID=(.*?);PASSWORD=(.*?);.*"); //serverName = m.Groups[1].Value; //dbName = m.Groups[2].Value; userName = m.Groups[3].Value; password = m.Groups[4].Value; } 

Is there any accepted practice here?

+15
c # mysql connection-string
Apr 02 2018-12-12T00:
source share
1 answer

You can use SqlConnectionStringBuilder Class

 string conString = "SERVER=localhost;DATABASE=tree;UID=root;PASSWORD=branch;Min Pool Size = 0;Max Pool Size=200"; SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(conString); string user = builder.UserID; string pass = builder.Password; 
+38
Apr 02 2018-12-12T00:
source share



All Articles