DbConnectionStringBuilder.ConnectionString 属性

定义

获取或设置与 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

属性值

String

使用 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();
}
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

注解

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

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

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

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

适用于

另请参阅