Monday, August 01, 2005

ADO.NET Basics: Connection String and Web.Config

Ok, on our continueing quest for world domination... oh wait wrong blog for that.

No we want to talk about connection strings. Fun stuff isn't it. Well in the last bit we showed you how to make the connection string in C#. Well you know after a while that can get annoying making that for every page so there is a solution. You can put it into one file, and use it across your whole application. You can put it into your Web.Config file. Then when you want to connect just use the string that you declared it as for the connection. So lets see how to do it.

First open your web config file and type:
< appsettings >
< / appsettings >


Inside this is where you will set the string so you can make the connection. Now type in:
< key="ConnectionString" value="server=myServer; database=Northwind; uid=User; pwd=Password;">

Now the connections string is ConnectionString and you can use it on any page you need to. The final result in your web.config file should look like

< appsettings >
< key="ConnectionString" value="server=myServer; database=Northwind; uid=User; pwd=Password;">
< / appsettings >

See that was painless, mostly.


Buddy Lindsey

p.s. I had to put a space between the wickets and slashes because the blog has a hard time posting them. You will need to be sure to add this if you decide to copy and paste the code.

0 Comments:

Post a Comment

<< Home