共用方式為


ConnectionStringSettingsCollection.Item[] 屬性

定義

取得或設定集合中的 ConnectionStringSettings 物件。

多載

Item[Int32]

取得或設定位於集合中所指定索引處的連接字串。

Item[String]

取得或設定集合中具有指定名稱的 ConnectionStringSettings 物件。

Item[Int32]

來源:
ConnectionStringSettingsCollection.cs
來源:
ConnectionStringSettingsCollection.cs
來源:
ConnectionStringSettingsCollection.cs

取得或設定位於集合中所指定索引處的連接字串。

public:
 property System::Configuration::ConnectionStringSettings ^ default[int] { System::Configuration::ConnectionStringSettings ^ get(int index); void set(int index, System::Configuration::ConnectionStringSettings ^ value); };
public System.Configuration.ConnectionStringSettings this[int index] { get; set; }
member this.Item(int) : System.Configuration.ConnectionStringSettings with get, set
Default Public Property Item(index As Integer) As ConnectionStringSettings

參數

index
Int32

集合中 ConnectionStringSettings 物件的索引。

屬性值

位於指定索引處的 ConnectionStringSettings 物件。

範例

下列範例示範如何存取 ConnectionStringSettings 集合中指定索引處的物件 ConnectionStringSettingsCollection

static void GetItems()
{

    try
    {
        System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration(
         ConfigurationUserLevel.None);

        // Clear the connection strings collection.
        ConnectionStringsSection csSection =
            config.ConnectionStrings;
        ConnectionStringSettingsCollection csCollection =
         csSection.ConnectionStrings;

        // Get the connection string setting element
        // with the specified index.
        ConnectionStringSettings cs =
            csCollection[0];

        Console.WriteLine(
             "cs: {0}", cs.Name);
    }
    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine(err.ToString());
    }
}
Shared Sub GetItems() 
    
    Try
        ' Get the application configuration file.
        Dim config _
        As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)

        ' Clear the connection strings collection.
        Dim csSection _
        As ConnectionStringsSection = _
        config.ConnectionStrings
        Dim csCollection _
        As ConnectionStringSettingsCollection = _
        csSection.ConnectionStrings
        
        ' Get the connection string setting element
        ' with the specified index.
        Dim cs _
        As ConnectionStringSettings = _
        csCollection(0)
        
        Console.WriteLine("cs: {0}", cs.Name)
    
    Catch err As ConfigurationErrorsException
        Console.WriteLine(err.ToString())
    End Try

End Sub

備註

在 C# 中,這個屬性是 ConnectionStringSettingsCollection 類別的索引子。

另請參閱

適用於

Item[String]

來源:
ConnectionStringSettingsCollection.cs
來源:
ConnectionStringSettingsCollection.cs
來源:
ConnectionStringSettingsCollection.cs

取得或設定集合中具有指定名稱的 ConnectionStringSettings 物件。

public:
 property System::Configuration::ConnectionStringSettings ^ default[System::String ^] { System::Configuration::ConnectionStringSettings ^ get(System::String ^ name); };
public System.Configuration.ConnectionStringSettings this[string name] { get; }
member this.Item(string) : System.Configuration.ConnectionStringSettings
Default Public ReadOnly Property Item(name As String) As ConnectionStringSettings

參數

name
String

集合中 ConnectionStringSettings 物件的名稱。

屬性值

具有指定之名稱的 ConnectionStringSettings 物件,否則為 null

範例

下列範例示範如何存取集合中的ConnectionStringSettingsCollection具名ConnectionStringSettings物件。

static void GetItems2()
{

    try
    {
        System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration(
         ConfigurationUserLevel.None);

        // Clear the connection strings collection.
        ConnectionStringsSection csSection =
            config.ConnectionStrings;
        ConnectionStringSettingsCollection csCollection =
         csSection.ConnectionStrings;

        // Get the connection string setting element
        // with the specified name.
        ConnectionStringSettings cs =
            csCollection["ConnStr0"];
       
        Console.WriteLine(
            "cs: {0}", cs.Name);
    }
    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine(err.ToString());
    }
}
Shared Sub GetItems2() 
    
    Try
        ' Get the application configuration file.
        Dim config _
        As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)

        ' Clear the connection strings collection.
        Dim csSection _
        As ConnectionStringsSection = _
        config.ConnectionStrings
        Dim csCollection _
        As ConnectionStringSettingsCollection = _
        csSection.ConnectionStrings
        
        ' Get the connection string setting element
        ' with the specified name.
        Dim cs _
        As ConnectionStringSettings = _
        csCollection("ConnStr0")
        
        Console.WriteLine("cs: {0}", cs.Name)
    
    Catch err As ConfigurationErrorsException
        Console.WriteLine(err.ToString())
    End Try

End Sub

備註

在 C# 中,這個屬性是 ConnectionStringSettingsCollection 類別的索引子。

另請參閱

適用於