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
单个条目。