Accessing the ConnectionString in ASP.NET 2.0
It is a general practise; we define the connection string in the web.config file and access it from else where of the application when we need to handle database activities. In asp.net 1.1 and lower versions; in web.config file we define the connection string by specifying it as a key value pair tag under the
And access the connection string as follows. string ConnectionString = Configuration.AppSettings[“ConnectionString”].ToString(); The asp.net2.0 takes a one step forward and changes this by adding a
This is how we could access this connection defined in the web.config file. string strCon = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString; The asp.net 2.0 uses the ConfigurationManager class which exposes the property of ConnnectionStrings where we could specify the key and access the connection string. If we want to get the application settings we could use the ConfigurationManager.AppSettings where this will give us the application settings of the web application.
Inorder to access the ConfigurationManager class we need to add the System.Configuration dll manually.
As you’ve seen now; ConfigurationManager class which is a new introduction in asp.net 2.0 allows us to carry out the general tasks in a bit different way; but guess; it’s trying to put things in to right places. Is it?
0 Comments:
Post a Comment
<< Home