DbConnectionStringBuilder.ConnectionString Property

Definition

获取或设置与 DbConnectionStringBuilder 相关联的连接字符串。Gets or sets the connection string associated with the DbConnectionStringBuilder.

public:
 property System::String ^ ConnectionString { System::String ^ get(); void set(System::String ^ value); };
public string ConnectionString { get; set; }
member this.ConnectionString : string with get, set
Public Property ConnectionString As String

Property Value

String

使用 DbConnectionStringBuilder 中包含的键/值对创建的当前连接字符串。The current connection string, created from the key/value pairs that are contained within the DbConnectionStringBuilder. 默认值为空字符串。The default value is an empty string.

Exceptions

提供的连接字符串参数无效。An invalid connection string argument has been supplied.

Examples

下面的示例演示 ConnectionString 属性的可能行为。The following example demonstrates possible behaviors of the ConnectionString property. 示例:The example:

  • 通过将键/值对逐个添加到空 DbConnectionStringBuilder来创建一个连接字符串。Creates a connection string by adding key/value pairs, one at a time, to an empty DbConnectionStringBuilder.

  • DbConnectionStringBuilder 实例的 ConnectionString 属性分配一个完整的连接字符串,并修改字符串中的单个键/值对。Assigns a complete connection string to the ConnectionString property of the DbConnectionStringBuilder instance, and modifies a single key/value pair within the string.

  • 将任意一组键/值对分配到 ConnectionString 属性(即,不像连接字符串那样远程操作的字符串),并修改其中一个值。Assigns an arbitrary set of key/value pairs to the ConnectionString property (that is, a string that is not anything remotely like a connection string), and modifies one of the values.

  • 将一个无效的连接字符串分配给 ConnectionString 属性,其中演示了引发的异常。Assigns an invalid connection string to the ConnectionString property, demonstrating the exception that is thrown.

Note

该示例包括一个密码以演示 DbConnectionStringBuilder 如何使用连接字符串。This example includes a password to demonstrate how DbConnectionStringBuilder works with connection strings. 在您的应用程序中,建议使用 Windows 身份验证。In your applications, we recommend that you use Windows Authentication. 如果必须使用密码,请不要在你的应用程序中包括硬编码的密码。If you must use a password, do not include a hard-coded password in your application.

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();
}
Sub Main()
    ' Create a new DbConnctionStringBuilder, and add items
    ' to the internal collection of key/value pairs.
    Dim builder As 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 ex As ArgumentException
        Console.WriteLine("Invalid connection string.")
    End Try

    Console.WriteLine()
    Console.WriteLine("Press Enter to finish.")
    Console.ReadLine()
End Sub

Remarks

此属性返回一个以分号分隔的键/值对列表,这些键/值对存储在由 DbConnectionStringBuilder维护的集合中。This property returns a semicolon-delimited list of key/value pairs stored within the collection maintained by the DbConnectionStringBuilder. 每个对都包含用等号分隔的键和值。Each pair contains the key and value, separated by an equal sign. 下面的示例演示了一个典型的连接字符串。The following example illustrates a typical connection string.

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

数据访问接口可能需要每个连接字符串属性的特定键和值。Data providers may expect specific keys and values for each connection string property. ,这些值将单独记录。, These values are documented individually. 虽然 DbConnectionStringBuilder 类不会验证与其连接字符串关联的键/值对,但从它继承的类可以。The DbConnectionStringBuilder class does not validate the key/value pairs associated with its connection string, although classes that inherit from it can.

DbConnectionStringBuilder 类的 ConnectionString 属性通常用作一种机制,用于创建和分析以等号分隔的键/值对列表(以分号分隔)。The ConnectionString property of the DbConnectionStringBuilder class acts generally as a mechanism for creating and parsing semicolon-delimited lists of key/value pairs separated with equal signs. 它不提供特定于连接字符串的任何验证或其他支持。It provides no validation or other support specific to connection strings. 如果将项添加到 DbConnectionStringBuilder 集合,则 ConnectionString 属性将反映所做的更改。If you add items to the DbConnectionStringBuilder collection, the ConnectionString property will reflect the changes. 如果为 ConnectionString 属性指定值,则 DbConnectionStringBuilder 将尝试使用分号和等号分隔符来分析值。If you assign a value to the ConnectionString property, the DbConnectionStringBuilder will try to parse the value, using the semicolon and equal-sign delimiters.

Applies to

See also