Saturday, July 11, 2009
Connection String in Asp.net
Connection string in asp.net c#.In Asp.net there is an easier way give the Connection String.we can add more than one Connection String in the web.config file.Then we can access these Connection Strings in any of our Pages.This is a simply superb concept in Asp.net. for example,You have two
Connection Strings one for your local server and another one for your live server.You simply add these two Connections in the web.config file and access the first connection in one page and in another page you can use the next connection.
ok, how to save this?
Well,Programming with C# is very easier when compared to other languages.give the coding below in your web.config file,
in configuration section,
<add name="Conn" connectionstring="server=.;database=Northwind;Integrated Security=True;"/>
what is server?
may be the server is your localhost or your web server,if localhost means you just give the '.' or if your server is web server means just give the web server address 'XXX.XX.XXX'.
what is database?
database is the collection of data stored in a system.Its having a unique name.
what is Integrated Security?
if you are working in localhost means you dont need to give your userid and password.you just give the Integrated Security=True.
if you are using a webserver means the connection string will be slightly change,
<add name="Conn" connectionstring="server=XXX.XXX.XX;database=Northwind;uid=yourserveruserid;pwd=yourserverpassword;/>
what is uid and pwd?
uid is the userID when the sql installation you will give the userid and password.those userid and password will give here.
then,next step how to access this in our .aspx.cs file
add this namespace
then add this in
SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString.ToString());
is used for connection opening
is used for connection closing.
This is the way of accessing the Connection Strings in all the pages.