ConnectionStringSettings 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示連接字串的組態檔區段中的單一具名連接字串。
public ref class ConnectionStringSettings sealed : System::Configuration::ConfigurationElement
public sealed class ConnectionStringSettings : System.Configuration.ConfigurationElement
type ConnectionStringSettings = class
inherit ConfigurationElement
Public NotInheritable Class ConnectionStringSettings
Inherits ConfigurationElement
- 繼承
範例
下列範例示範如何存取 ConnectionStringSettings 集合中指定索引處 ConnectionStringSettingsCollection 的物件。
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Configuration;
using System.Collections;
using System.Text;
namespace ConfigurationStringSettings
{
class ConfigurationStringSettings
{
static void DisplayConnectionStrings()
{
// Set the path of the config file.
// Make sure that you have a Web site on the
// same server called TestConfig.
string configPath = "/TestConfig";
// Get the Web application configuration object.
Configuration config =
WebConfigurationManager.OpenWebConfiguration(configPath);
// Get the conectionStrings section.
ConnectionStringsSection csSection =
config.ConnectionStrings;
Console.WriteLine("Display configuration strings.");
for (int i = 0; i <
ConfigurationManager.ConnectionStrings.Count; i++)
{
ConnectionStringSettings cs =
csSection.ConnectionStrings[i];
Console.WriteLine(" Connection String: \"{0}\"",
cs.ConnectionString);
Console.WriteLine("#{0}", i);
Console.WriteLine(" Name: {0}", cs.Name);
Console.WriteLine(" Provider Name: {0}",
cs.ProviderName);
}
}
static void Main(string[] args)
{
try
{ // Display connection strings.
DisplayConnectionStrings();
}
catch (Exception e)
{
// Unknown error.
Console.WriteLine(e.ToString());
}
// Display and wait.
Console.WriteLine("Enter any key to exit.");
Console.ReadLine();
}
}
}
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Collections
Imports System.Text
Namespace ConfigurationStringSettings
Class ConfigurationStringSettings
Shared Sub DisplayConnectionStrings()
' Set the path of the config file.
' Make sure that you have a Web site on the
' same server called TestConfig.
Dim configPath As String = "/TestConfig"
' Get the Web application configuration object.
Dim config As Configuration = _
WebConfigurationManager.OpenWebConfiguration(configPath)
' Get the conectionStrings section.
Dim csSection As ConnectionStringsSection = _
config.ConnectionStrings
Console.WriteLine("Display configuration strings.")
Dim i As Integer = 0
While i < ConfigurationManager.ConnectionStrings.Count
Dim cs As ConnectionStringSettings = _
csSection.ConnectionStrings(i)
Console.WriteLine(" Connection String: ""{0}""", _
cs.ConnectionString)
Console.WriteLine("#{0}", i)
Console.WriteLine(" Name: {0}", cs.Name)
Console.WriteLine(" Provider Name: {0}", cs.ProviderName)
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
End Sub
Shared Sub Main(ByVal args As String())
Try
' Display connection strings.
DisplayConnectionStrings()
Catch e As Exception
' Unknown error.
Console.WriteLine(e.ToString())
End Try
' Display and wait.
Console.WriteLine("Enter any key to exit.")
Console.ReadLine()
End Sub
End Class
End Namespace
備註
ConnectionStringSettings物件代表組態檔區段中的單一專案connectionStrings
。
建構函式
ConnectionStringSettings() |
初始化 ConnectionStringSettings 類別的新執行個體。 |
ConnectionStringSettings(String, String) |
初始化 ConnectionStringSettings 類別的新執行個體。 |
ConnectionStringSettings(String, String, String) |
初始化 ConnectionStringSettings 類別的新執行個體。 |