ConfigurationManager.ConnectionStrings 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取当前应用程序的默认配置的 ConnectionStringsSection 数据。
public:
static property System::Configuration::ConnectionStringSettingsCollection ^ ConnectionStrings { System::Configuration::ConnectionStringSettingsCollection ^ get(); };
public static System.Configuration.ConnectionStringSettingsCollection ConnectionStrings { get; }
static member ConnectionStrings : System.Configuration.ConnectionStringSettingsCollection
Public Shared ReadOnly Property ConnectionStrings As ConnectionStringSettingsCollection
属性值
当前应用程序的默认配置 ConnectionStringsSection 对象的内容。
例外
示例
以下示例演示如何使用连接字符串从数据库读取数据。
using System;
using System.Configuration;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ReadProducts();
}
static void ReadProducts()
{
var connectionString = ConfigurationManager.ConnectionStrings["WingtipToys"].ConnectionString;
string queryString = "SELECT Id, ProductName FROM dbo.Products;";
using (var connection = new SqlConnection(connectionString))
{
var command = new SqlCommand(queryString, connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1]));
}
}
}
}
}
}
Imports System.Configuration
Imports System.Data.SqlClient
Module Module1
Sub Main()
ReadProducts()
End Sub
Sub ReadProducts()
Dim connectionString = ConfigurationManager.ConnectionStrings("WingtipToys").ConnectionString
Dim queryString = "SELECT Id, ProductName FROM dbo.Products;"
Using connection As New SqlConnection(connectionString)
Dim command = New SqlCommand(queryString, connection)
connection.Open()
Using reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
Console.WriteLine(String.Format("{0}, {1}", reader(0), reader(1)))
End While
End Using
End Using
End Sub
End Module
注解
ConnectionStringsSection 对象包含配置文件 connectionStrings
节的内容。