OleDbConnectionStringBuilder.Item[String] 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置与指定键关联的值。 在 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 是 null 引用(Nothing 在 Visual Basic 中)。
示例
下面的示例使用 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”值。 有关演示,请参阅本主题中的示例。