How to get Connection String From the Web.config [

2019-09-21 12:28发布

问题:

Possible Duplicate:
In C# , how can I read a connection string stored in my web.config file connection string?

I am using the SqlConnection class to connect to a sql server database. One of the constructors for this class required a connection string argument...What is the easiest way to get the required information from the web.config file....

回答1:

Use ConfigurationManager.ConnectionStrings collection to read connection string from web.config.



回答2:

connection string in web config

import this

using System.Configuration;

in aspx page

SqlConnection con;

string conn = ConfigurationManager.ConnectionStrings["connection string name in web config"].ConnectionString;


回答3:

Try this:

First include this Namespace using System.Configuration;

and use this

string cnn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;