2012-04-02 5 views
15

私はこのような接続文字列があります。私はそれのうちさまざまなデータベースパラメータを取得するにはどうすればよい接続文字列からユーザー名とパスワードを取得する正しい方法は?

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

を?私はこのようにデータベース名とサーバーを得ることができます:

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

同様に同様のユーザー名とパスワードが必要です。 MySqlConnectionオブジェクトにはプロパティが設定されていません。現時点では

私はこのようにそれを行う:

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; 
} 

は、ここでは慣例はありますか?

答えて

39

あなたがSqlConnectionStringBuilderクラス

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; 
+0

にもmysqlのためにも、この作業を行うを使用することができますか? – nawfal

+1

MySQL用に特別な.NETコンポーネントを使用する場合、特定のConnectionStringBuilderが必要です。 MySqlConnectionStringBuilder。 – Devart

+0

@Devartありがとう..今私はそれを得る.. – nawfal

関連する問題