OleDbConnectionStringBuilder.Item[String] 属性

定义

获取或设置与指定的键关联的值。 在 C# 中,此属性为索引器。

public:
 virtual property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ keyword); void set(System::String ^ keyword, System::Object ^ value); };
public override object this[string keyword] { get; set; }
member this.Item(string) : obj with get, set
Default Public Overrides Property Item(keyword As String) As Object

参数

keyword
String

要获取或设置的项的键。

属性值

与指定的键相关联的值。

例外

连接字符串的格式错误(可能是键/值对中缺少必需的“=”)。

keyword 为空引用(在 Visual Basic 中为 Nothing)。

示例

以下示例使用 Item[] C#) 中索引器 (属性来检索和设置键/值对集合中的值。 请注意,在本例中,设置提供程序还会为与所选提供程序关联的所有键/值对提供默认值。

using System.Data.OleDb;

class Program
{
    static void Main()
    {
        OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
        builder.Provider = "Microsoft.Jet.Oledb.4.0";
        builder.DataSource = @"C:\Sample.mdb";
        // Set properties using the Item property (the indexer, in C#).
        builder["Jet OLEDB:Encrypt Database"] = true;
        builder["Jet OLEDB:System database"] = @"C:\Workgroup.mdw";

        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();

        // Use the Item property to retrieve values as well.
        Console.WriteLine(builder["Jet OLEDB:System database"]);
        Console.WriteLine(builder["Jet OLEDB:Encrypt Database"]);

        // You can set or retrieve any of the "default" values for the
        // provider, even if you didn't set their values.
        Console.WriteLine(builder["Jet OLEDB:Database Locking Mode"]);
        Console.WriteLine(builder["Jet OLEDB:Global Partial Bulk Ops"]);

        Console.WriteLine("Press Enter to continue.");
        Console.ReadLine();
    }
}
Imports System.Data.OleDb

Module Module1
  Sub Main()
    Dim builder As New OleDbConnectionStringBuilder
    builder.Provider = "Microsoft.Jet.Oledb.4.0"
    builder.DataSource = "C:\Sample.mdb"
    ' Set properties using the Item property.
    builder.Item("Jet OLEDB:Encrypt Database") = True

    ' Because Item is the default property, you can leave out
    ' the explicit reference.
    builder("Jet OLEDB:System database") = "C:\Workgroup.mdw"

    Console.WriteLine(builder.ConnectionString)
    Console.WriteLine()

    ' Use the Item property to retrieve values, as well.
    Console.WriteLine(builder.Item("Jet OLEDB:System database"))
    Console.WriteLine(builder("Jet OLEDB:Encrypt Database"))

    ' You can set or retrieve any of the "default" values for the
    ' provider, as well, even if you did not set their values. Again,
    ' explicitly specifying the Item property name is optional.
    Console.WriteLine(builder.Item("Jet OLEDB:Database Locking Mode"))
    Console.WriteLine(builder("Jet OLEDB:Global Partial Bulk Ops"))

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

注解

由于设置 属性 Provider 可能会将相应的项添加到键/值对集合 (,具体取决于特定提供程序) 的行为,因此,您可能能够检索尚未显式设置的键的值。 例如,将 属性设置为 Provider “sqloledb”后,即使尚未自行设置,也可以检索“工作站 ID”值。 有关演示,请参阅本主题中的示例。

适用于

另请参阅