OleDbConnectionStringBuilder.Item[String] 属性
定义
获取或设置与指定的键关联的值。Gets or sets the value associated with the specified key. 在 C# 中,此属性为索引器。In C#, this property is the indexer.
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
要获取或设置的项的键。The key of the item to get or set.
属性值
与指定的键相关联的值。The value associated with the specified key.
例外
连接字符串的格式错误(可能是键/值对中缺少必需的“=”)。The connection string is incorrectly formatted (perhaps missing the required "=" within a key/value pair).
keyword 为空引用(在 Visual Basic 中为 Nothing)。keyword is a null reference (Nothing in Visual Basic).
示例
下面的示例使用 Item[] c # ) (索引器的属性来检索和设置键/值对集合中的值。The following example uses the Item[] property (the indexer, in C#) to retrieve and set values within the collection of key/value pairs. 请注意,在这种情况下,设置提供程序还为与所选提供程序关联的所有键/值对提供默认值。Note that setting the provider, in this case, also provides default values for all the key/value pairs associated with the selected provider.
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:Database Password"] = "DataPassword";
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:Database Password") = "DataPassword"
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 属性可能会将相应的项添加到键/值对的集合中 (根据特定提供程序) 的行为,因此您可能能够检索尚未显式设置的键的值。Because setting the Provider property may add corresponding items to the collection of key/value pairs (depending on the behavior of the specific provider), you may be able to retrieve a value for a key you have not set explicitly. 例如,一旦您将 Provider 属性设置为 "sqloledb",就可以检索 "WORKSTATION ID" 值,即使您并未自行设置。For example, as soon as you have set the Provider property to "sqloledb," you can retrieve the "Workstation ID" value even if you have not set it yourself. 有关演示,请参阅本主题中的示例。See the example in this topic for a demonstration.