Configuration.ConnectionStrings 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取应用于此 Configuration 对象的 ConnectionStringsSection 配置节对象。
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
在打开的配置文件中定义的配置节。