Home > ASP.NET > How to maintain ASP.NET Connection string in Web.config

How to maintain ASP.NET Connection string in Web.config

December 22nd, 2009 Shyju Leave a comment Go to comments

Some time we need to store connection strings in web.config file .The below is the code to add connection string to web.config.file

<configuration> <appSettings> <add key=”ConnectionString” value=”server=localhost;uid=sa;pwd=;database=testdatabase” /> </appSettings> </configuration>

You can change the connection string according to your requirement.here i am specifying a connectionstring for accessing data from the same server (therefor server : localhost) and the database name is testdatabase.

How do we access this from our ASP.NET pages ?     This way it is

We need to include System.Configuration namespace
using System.Configuration;
string connectionString = (string )ConfigurationSettings.AppSettings["ConnectionString"];

Categories: ASP.NET Tags:
  1. December 27th, 2009 at 08:29 | #1

    Just a quick question. How wil you use Windows Integrated Authenticatio with ? Why cant you use instead which has option for integrated security.

  2. Shyju
    December 27th, 2009 at 22:58 | #2

    We can use Windows Integrated Security too to create a connection.At that time we would be using windows user credentials. But in a scenario where web server (infact IIS ) is different from the DB server, This is not ideal to use

  1. No trackbacks yet.