OdbcConnectionStringBuilder.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)。

示例

以下代码在控制台应用程序中新建一个 OdbcConnectionStringBuilder,并使用 Item[] 属性将键/值对添加到其连接字符串中。

using System.Data.Odbc;

class Program
{
    static void Main()
    {
        OdbcConnectionStringBuilder builder =
            new OdbcConnectionStringBuilder();
        // Set up a connection string for a text file.
        builder["Driver"] = "Microsoft Text Driver (*.txt; *.csv)";
        builder["dbq"] = "C:\\TextFilesFolder";
        builder["Extension"] = "asc,csv,tab,txt";

        // Overwrite the existing value for the dbq value,
        // because it already exists within the collection.
        builder["dbq"] = "D:\\";

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

Module Module1
  Sub Main()
    Dim builder As New OdbcConnectionStringBuilder
    ' Set up a connection string for a text file.
    builder.Item("Driver") = "Microsoft Text Driver (*.txt; *.csv)"
    ' Note that Item is the default property, so 
    ' you need not include it in the reference.
    builder("dbq") = "C:\TextFilesFolder"
    builder.Item("Extension") = "asc,csv,tab,txt"

    ' Overwrite the existing value for the dbq value, 
    ' because it already exists within the collection.
    builder.Item("dbq") = "D:\"

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

注解

设置此属性时,如果字典中已存在指定的键,则替换值;否则,将创建一个新元素。

适用于

另请参阅