共用方式為


Configuration.ConnectionStrings 屬性

定義

取得此 ConnectionStringsSection 物件所套用的 Configuration 組態區段物件。

public:
 property System::Configuration::ConnectionStringsSection ^ ConnectionStrings { System::Configuration::ConnectionStringsSection ^ get(); };
public System.Configuration.ConnectionStringsSection ConnectionStrings { get; }
member this.ConnectionStrings : System.Configuration.ConnectionStringsSection
Public ReadOnly Property ConnectionStrings As ConnectionStringsSection

屬性值

ConnectionStringsSection 組態區段物件,表示此 Configuration 物件所套用的 connectionStrings 組態區段。

範例

下列程式代碼範例示範如何使用 ConnectionStrings 屬性。

ConnectionStringsSection
    conStrSection =
    config.ConnectionStrings as ConnectionStringsSection;
Console.WriteLine("Section name: {0}",
    conStrSection.SectionInformation.SectionName);

try
{
    if (conStrSection.ConnectionStrings.Count != 0)
    {
        Console.WriteLine();
        Console.WriteLine("Using ConnectionStrings property.");
        Console.WriteLine("Connection strings:");

        // Get the collection elements.
        foreach (ConnectionStringSettings connection in
          conStrSection.ConnectionStrings)
        {
            string name = connection.Name;
            string provider = connection.ProviderName;
            string connectionString = connection.ConnectionString;

            Console.WriteLine("Name:               {0}",
              name);
            Console.WriteLine("Connection string:  {0}",
              connectionString);
            Console.WriteLine("Provider:            {0}",
               provider);
        }
    }
}
catch (ConfigurationErrorsException e)
{
    Console.WriteLine("Using ConnectionStrings property: {0}",
        e.ToString());
}
Dim conStrSection As ConnectionStringsSection = TryCast(config.ConnectionStrings, ConnectionStringsSection)
Console.WriteLine("Section name: {0}", conStrSection.SectionInformation.SectionName)

Try
    If conStrSection.ConnectionStrings.Count <> 0 Then
        Console.WriteLine()
        Console.WriteLine("Using ConnectionStrings property.")
        Console.WriteLine("Connection strings:")

        ' Get the collection elements.
        For Each connection As ConnectionStringSettings In conStrSection.ConnectionStrings
            Dim name As String = connection.Name
            Dim provider As String = connection.ProviderName
            Dim connectionString As String = connection.ConnectionString

            Console.WriteLine("Name:               {0}", name)
            Console.WriteLine("Connection string:  {0}", connectionString)
            Console.WriteLine("Provider:            {0}", provider)
        Next connection
    End If
Catch e As ConfigurationErrorsException
    Console.WriteLine("Using ConnectionStrings property: {0}", e.ToString())
End Try

備註

ConnectionStrings使用 屬性來存取和變更connectionStrings開啟組態檔中定義的組態區段。

適用於