Various Ways To Specify Connection String In ASP.NET Web And Windows Applications

This article describes various ways to declare a connection string. We will see the following points.

  1. Specify Database connection string in Web Application.
  2. Specify Database connection string in Windows application.

Let’s start from the first point.

Specify Database connection string in Web Application

 

In a web application we can specify a database connection string in one of the following two ways.

  • Specify it in the web.config file.
  • Create a common class file for the connection string.

Write connection string in web.config file

 

The benefit of writing a connection string in a web config file is there is no need to write code every time to connect to the database. It reduces the redundancy of code over a single application. One more benefit of this is whenever we need to change the database name or credentials we need to change only the web config file.

Now I will describe how to specify a connection string in a web.config file.

Go to the Web.config file as in the following:

specify connection string

You need to use the connectionStrings tag to specify the connection string in the web.config file as in the following image.

connection string

Write this XML markup in the web config file for declaration of the connection. You can add many tags for each database connection.

 
  1. <connectionStrings>  
  2.    <add name="mytest" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=mytest;Integrated Security=True;" providerName="System.Data.SqlClient" />  
  3.    <add name="Northwind" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;" providerName="System.Data.SqlClient" />  
  4.    <add name="Northwind" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=databasename;uid=sa;pwd=sa;" providerName="System.Data.SqlClient" />  
  5. </connectionStrings>  

Read Connection string from Web.config file


Now I will describe how to read a web.config connection string in an ASP.NET web application.

  • You need to add class files to an ASP.Net web application.
  • Then you need to include a namespace using System.Configuration in the class file.

    namespace

Then write the following method in this class.

Configuration

 
  1. public static string Getconnectionstring(string keyname)  
  2. {  
  3.     string connection = string.Empty;  
  4.     switch (keyname)  
  5.     {  
  6.         case "mytest":  
  7.             connection = ConfigurationManager.ConnectionStrings["mytest"].ConnectionString;  
  8.             break;  
  9.         case "Northwind":  
  10.             connection = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;  
  11.             break;  
  12.         default:  
  13.             break;  
  14.     }  
  15.     return connection;  
  16.   
  17. }  

In the preceding I have written a method to get the connection on the basis of the keyname passed.

Then I will show you how to use the preceding method of this class in code to access connections. In the following code you can see I have used the GetConnectionstring method of the ConnectionString class and passed the keyname to connect to the specific database using keyname.

method

If you got this error when reading a connection string from a web.config file then go to this link: Click here to fix the following error.

erroe

Using the preceding described way you can connect to the database using this method, there is no need to write a connection code in every page.

Now we will see the next way to specify a connection.

Now I will create a common class file with connection code.

I have created a DBConnectionClass.cs file and then written the following code in this class. You can use this class file in multiple Web applications where you need to use the same database connection.

In this class I declared an enum for the list of the database. We can specify all the database names to be used across application and access them using an enum.

Then I wrote a method, GetConnectionstring, with Database enum parameter.

code

Write the following code in your class:

 
  1. /// <summary>  
  2.  /// list of database name  
  3.  /// </summary>  
  4.  public enum Database  
  5.  {  
  6.      MyTest,  
  7.      Northwind  
  8.  }  
  9.  /// <summary>  
  10.  /// This method return connction string  
  11.  /// </summary>  
  12.  /// <param name="DBName">pass Dbname using enum</param>  
  13.  /// <returns></returns>  
  14.  public static SqlConnection Getconnectionstring(Database DBName)  
  15.  {  
  16.      SqlConnection connection=null;  
  17.      switch (DBName)  
  18.      {  
  19.          case Database.MyTest:  
  20.              connection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=mytest;Integrated Security=True");  
  21.              break;  
  22.          case Database.Northwind:  
  23.              connection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");  
  24.              break;  
  25.          default:  
  26.              break;  
  27.      }  
  28.      return connection;  
  29.   
  30.  }  

Then I will describe how to access this class method in a web page.

You can call this method as in DBConnectionClass.Getconnectionstring(DBConnectionClass.Database.Northwind).

GetConnectionstring
 

Specify Database connection string in Windows application

 

Now I will describe how to declare a connection string in an App.config file of a Windows application.

 

 

In the same way you saw in the web.config file you need to write this tag inside the app.config file.

DBConnectionClass

In the same way I already described, for the web application you can read a connection from the app.config.

But one extra thing you need to do in Windows applications is that you need to add a reference of System.Configuration to your application.

app config

Then you need to follow the steps I already described for web applications.

You can also add a common DBConnectionClass class in the Windows application for the connection in the same way that I described in the Web section of this article.


Similar Articles