读取连接字符串参数

Read Connection String Parameters

想知道是否有办法读取 web.config 文件中连接字符串的某些参数?例如,如果我的网站上有一个仅供管理员使用的 Settings 页面,其中的一个视图显示名称、providerName 和 connectionString...我可以这样做吗?

<add 
name="DbConnectionString" 
providerName="System.Data.SqlClient" 
connectionString="Data Source=yourservernamehere.net 
/>

第一步是确保声明 using System.Configuration

SqlConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnectionString"].ToString());

lblDBConnName.Text = dbConn.DataSource.ToString();

Curious to know if there is a way to read in some parameters of the connectionstring within the web.config file?

.

For example, if I had a Settings page on my website for administrators only, and one of the views within there was to show the name, providerName, and connectionString... could I do this?

是的。你也可以这样做:

using System.Configuration;
string yourConnectionString = ConfigurationManager.ConnectionStrings["yourConnectionStringNameInYourConfigFile"].ConnectionString;