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 the
<connectionStrings>
<add name="Conn" connectionstring="server=.;database=Northwind;Integrated Security=True;"/>
</connectionStrings>

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,

<connectionStrings>
<add name="Conn" connectionstring="server=XXX.XXX.XX;database=Northwind;uid=yourserveruserid;pwd=yourserverpassword;/>
</connectionStrings>

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
using System.Data.Sqlclient

then add this in

SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString.ToString());

con.Open();
is used for connection opening

con.Close();
is used for connection closing.

This is the way of accessing the Connection Strings in all the pages.

1 comment:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai Dot Net Training in Chennai


    Dot Net Online Training Dot Net Online Training LINQ Online Training LINQ Online Training ASP.NET Online Training ASP.NET Online Training

    ReplyDelete

Followers