OleDbConnectionStringBuilder.Item[String] Proprietà

Definizione

Ottiene o imposta il valore associato alla chiave specificata. In C# questa proprietà è l'indicizzatore.

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

Parametri

keyword
String

Chiave dell'elemento da ottenere o impostare.

Valore della proprietà

Valore associato alla chiave specificata.

Eccezioni

La stringa di connessione è formattata in modo errato (possibile mancanza del segno "=" all'interno di una coppia chiave/valore).

keyword è un riferimento null (Nothing in Visual Basic).

Esempio

Nell'esempio seguente viene usata la Item[] proprietà (indicizzatore, in C#) per recuperare e impostare i valori all'interno della raccolta di coppie chiave/valore. Si noti che l'impostazione del provider, in questo caso, fornisce anche i valori predefiniti per tutte le coppie chiave/valore associate al provider selezionato.

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

Commenti

Poiché l'impostazione della Provider proprietà può aggiungere elementi corrispondenti alla raccolta di coppie chiave/valore (a seconda del comportamento del provider specifico), potrebbe essere possibile recuperare un valore per una chiave non impostata in modo esplicito. Ad esempio, non appena è stata impostata la Provider proprietà su "sqloledb", è possibile recuperare il valore "ID workstation" anche se non è stato impostato manualmente. Vedere l'esempio in questo argomento per una dimostrazione.

Si applica a

Vedi anche