接続文字列からユーザー名とパスワードを取得する正しい方法は? [重複] 質問する

接続文字列からユーザー名とパスワードを取得する正しい方法は? [重複] 質問する

次のような接続文字列があります:

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

ここでは受け入れられている慣行がありますか?

ベストアンサー1

あなたはSql接続文字列ビルダークラス

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;

おすすめ記事