Stephen85
Stephen85

Reputation: 270

Read Connection String Parameters

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?

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

Upvotes: 1

Views: 660

Answers (2)

Nguyen Van Thanh
Nguyen Van Thanh

Reputation: 825

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

Yes.

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?

Yes. You can do the same:

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

Upvotes: 4

Stephen85
Stephen85

Reputation: 270

First step was to ensure using System.Configuration is declared.

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

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

Upvotes: 0

Related Questions