OleDbConnectionStringBuilder.Item[String] Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the value associated with the specified key. 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
is a null reference (Nothing
in Visual Basic).
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: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
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. 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.
Produkt | Versions |
---|---|
.NET | 8 (package-provided), 9 (package-provided) |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1 |
.NET Standard | 2.0 (package-provided) |
.NET feedback
.NET er et åben kildekode projekt. Vælg et link for at give feedback: