需要在变量中显示连接信息

时间:2012-10-05 02:19:18

标签: c# ado.net

我有连接字符串

SqlConnection con = new SqlConnection();

其中con包含来自DLL或app.config的连接字符串信息。

现在我想用不同的变量显示每一件事。

e.g:

var connectionString = "Data Source=172.16.0.9;Initial Catalog=whiziblesem;UserID=mppuser;Password=mppuser";

预期产出:

        a = server name
        b = Database name
        c = username
        d = password

1 个答案:

答案 0 :(得分:5)

使用 SqlConnectionStringBuilder 类:

var builder = new SqlConnectionStringBuilder(connectionString);

用法:

Console.WriteLine("Data Source: {0}", builder.DataSource);
Console.WriteLine("Initial Catalog: {0}", builder.InitialCatalog);
Console.WriteLine("User ID: {0}", builder.UserID);
Console.WriteLine("Password: {0}", builder.Password);
///...