ConnectionStringsSection 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供对连接字符串配置文件部分的编程访问。
public ref class ConnectionStringsSection sealed : System::Configuration::ConfigurationSection
public sealed class ConnectionStringsSection : System.Configuration.ConfigurationSection
type ConnectionStringsSection = class
inherit ConfigurationSection
Public NotInheritable Class ConnectionStringsSection
Inherits ConfigurationSection
- 继承
示例
以下示例演示如何使用 ConnectionStringsSection。
// Create a connectionn string element and add it to
// the connection strings section.
static ConnectionStrings()
{
// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Get the current connection strings count.
int connStrCnt =
ConfigurationManager.ConnectionStrings.Count;
// Create the connection string name.
string csName =
"ConnStr" + connStrCnt.ToString();
// Create a connection string element and
// save it to the configuration file.
// Create a connection string element.
ConnectionStringSettings csSettings =
new ConnectionStringSettings(csName,
"LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" +
"Initial Catalog=aspnetdb", "System.Data.SqlClient");
// Get the connection strings section.
ConnectionStringsSection csSection =
config.ConnectionStrings;
// Add the new element.
csSection.ConnectionStrings.Add(csSettings);
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
}
' Create a connectionn string element and add it to
' the connection strings section.
Shared Sub New()
' Get the application configuration file.
Dim config _
As System.Configuration.Configuration = _
ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
' Get the current connection strings count.
Dim connStrCnt As Integer = _
ConfigurationManager.ConnectionStrings.Count
' Create the connection string name.
Dim csName As String = "ConnStr" + connStrCnt.ToString()
' Create a connection string element and
' save it to the configuration file.
' Create a connection string element.
Dim csSettings _
As New ConnectionStringSettings( _
csName, _
"LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" + _
"Initial Catalog=aspnetdb", "System.Data.SqlClient")
' Get the connection strings section.
Dim csSection _
As ConnectionStringsSection = _
config.ConnectionStrings
' Add the new element.
csSection.ConnectionStrings.Add(csSettings)
' Save the configuration file.
config.Save(ConfigurationSaveMode.Modified)
End Sub
以下摘录显示了上一示例使用的配置。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="ConnStr1" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
注解
ConnectionStringsSection 类允许以编程方式修改配置文件的 connectionStrings
部分。 连接字符串作为 ConnectionStringSettingsadd
配置元素的 ConnectionStringSettingsCollection 提供。
构造函数
ConnectionStringsSection() |
初始化 ConnectionStringsSection 类的新实例。 |