使用英语阅读

通过


DbConnectionStringBuilder.ConnectionString 属性

定义

获取或设置与 DbConnectionStringBuilder 相关联的连接字符串。

public string ConnectionString { get; set; }

属性值

使用 DbConnectionStringBuilder 中包含的键/值对创建的当前连接字符串。 默认值为一个空字符串。

例外

提供的连接字符串参数无效。

示例

以下示例演示 属性的 ConnectionString 可能行为。 示例:

备注

该示例包括一个密码以演示 DbConnectionStringBuilder 如何使用连接字符串。 在您的应用程序中,建议使用 Windows 身份验证。 如果必须使用密码,请不要在你的应用程序中包括硬编码的密码。

static void Main()
{
    // Create a new DbConnctionStringBuilder, and add items
    // to the internal collection of key/value pairs.
    DbConnectionStringBuilder builder = new
        DbConnectionStringBuilder();
    builder.Add("Data Source", @"c:\MyData\MyDb.mdb");
    builder.Add("Provider", "Microsoft.Jet.Oledb.4.0");
    builder.Add("Jet OLEDB:Database Password", "********");
    builder.Add("Jet OLEDB:System Database",
        @"c:\MyData\Workgroup.mdb");

    // Set up row-level locking.
    builder.Add("Jet OLEDB:Database Locking Mode", 1);
    // Display the contents of the connection string, which
    // will now contain all the key/value pairs delimited with
    // semicolons.
    Console.WriteLine(builder.ConnectionString);
    Console.WriteLine();
    // Clear the DbConnectionStringBuilder, and assign a complete
    // connection string to it, to demonstrate how
    // the class parses connection strings.
    builder.Clear();
    builder.ConnectionString =
        "Data Source=(local);Initial Catalog=AdventureWorks;"
        + "Integrated Security=SSPI";
    // The DbConnectionStringBuilder class has parsed the contents,
    // so you can work with any individual key/value pair.
    builder["Data Source"] = ".";
    Console.WriteLine(builder.ConnectionString);
    Console.WriteLine();
    // Because the DbConnectionStringBuilder class doesn't
    // validate its key/value pairs, you can use this class
    // to store any semicolon-delimited list. The following
    // snippet places an arbitrary string into the ConnectionString
    // property, changes one of the values, and then displays the
    // resulting string.
    builder.Clear();
    builder.ConnectionString =
        "Value1=10;Value2=20;Value3=30;Value4=40";
    builder["Value2"] = 25;
    Console.WriteLine(builder.ConnectionString);
    Console.WriteLine();

    builder.Clear();
    try
    {
        // Assigning an invalid connection string
        // throws an ArgumentException.
        builder.ConnectionString = "xxx";
    }
    catch (ArgumentException)
    {
        Console.WriteLine("Invalid connection string.");
    }

    Console.WriteLine();
    Console.WriteLine("Press Enter to finish.");
    Console.ReadLine();
}

注解

此属性返回以分号分隔的键/值对列表,这些键/值对存储在 由 DbConnectionStringBuilder维护的集合中。 每对都包含键和值,用等号分隔。 以下示例演示了典型的连接字符串。

"Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=AdventureWorks;Data Source=(local)"

数据提供程序可能需要每个连接字符串属性的特定键和值。 ,这些值单独记录。 类DbConnectionStringBuilder不验证与其连接字符串关联的键/值对,尽管从它继承的类可以。

ConnectionString类的 DbConnectionStringBuilder 属性通常充当一种机制,用于创建和分析用等号分隔的以分号分隔的键/值对列表。 它不提供特定于连接字符串的验证或其他支持。 如果将项添加到集合中 DbConnectionStringBuilder ,则 ConnectionString 属性将反映更改。 如果为 ConnectionString 属性赋值,将 DbConnectionStringBuilder 尝试使用分号和等号分隔符来分析该值。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

另请参阅