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 connection 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=True;" +
"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
備註
ConnectionStringsSection 類別可讓您以程序設計方式修改組態檔的 connectionStrings
區段。 連接字串會以 ConnectionStringSettingsadd
組態專案的 ConnectionStringSettingsCollection 的形式提供。
建構函式
ConnectionStringsSection() |
初始化 ConnectionStringsSection 類別的新實例。 |