Various ways to specify Connection String in Asp.Net Web Application and in Window Application


In this article I am going to describe various ways to declare connection string. We will see below point.
(1)    Specify Database connection string in Web Application
(2)    Specify Database  connection string in window application

Let’s start from first point.
(1) Specify Database connection string in Web Application
In web application we can specify database connection string in two different ways.
-One is specify in web.config file.
-Next we can create a common class file for connection string.

-Write connection string in web.config file
Benefit of writing connection string in web config file- If we declare connection string in web.config file no need to write code every time to connect to the database. Reduce redundancy of code over a single application.  One more benefit of this whenever we need to change database name or credential we need to change only web config file.   
Now I will describe how to specify connection string in web.config file.
Go to Web.config file


You need to use connectionStrings tag to specify connection string in web.config file. Like below image.
Write this XML markup in web config file for declaration of connection. You can add many tag for different database connection .


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


-Read Connection string from Web.config file
Now I will describe how to read web.config connection string in Asp.net web application.
-you need to add a class files in asp.net web application .
-Then you need to include namespace using System.Configuration; in class file.
 Then write this method in this class.



public static string Getconnectionstring(string keyname)
    {
        string connection = string.Empty;
        switch (keyname)
        {
            case "mytest":
                connection = ConfigurationManager.ConnectionStrings["mytest"].ConnectionString;
                break;
            case "Northwind":
                connection = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
                break;
            default:
                break;
        }
        return connection;

    }
 In above I have written a method to get connection on the basis of passed keyname.
 Then I will show you how to use above method of this class in code to access connection. In below code you can see I have used GetConnectionstring method  of ConnectionString class and passed keyname to connect to the particular database using keyname.

If you got this error when reading connection string from web.config file then go to this link

Using above described way you can connect to the database using this method no need to write connection code in every page.

Now we will see next way to specify connection.
Now I will create a common class file with connection code –
I have created DBConnectionClass.cs file and then write below code in this class. You can use this class file in multiple Web application where you need to use same database connection.
In this class I have declared enum for list of database. We can specify all database names which are used in across application and access through enum.
-Then I have written one method GetConnectionstring method with Database enum parameter.
 Write below code in your class

   /// <summary>
    /// list of database name
    /// </summary>
  public enum Database
    {
        MyTest,
        Northwind
    }
    /// <summary>
    /// This method return connction string
    /// </summary>
    /// <param name="DBName">pass Dbname using enum</param>
    /// <returns></returns>
    public static SqlConnection Getconnectionstring(Database DBName)
    {
        SqlConnection connection=null;
        switch (DBName)
        {
            case Database.MyTest:
                connection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=mytest;Integrated Security=True");
                break;
            case Database.Northwind:
                connection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");
                break;
            default:
                break;
        }
        return connection;

    }
Then I will describe how to access this class method in web page.
You can call this method like this DBConnectionClass.Getconnectionstring(DBConnectionClass.Database.Northwind)
 (2)    Specify Database connection string in window application

Now I will describe how to declare connection string in App.config file of window application.
In the same way as you seen in web.config file you need to write this tag inside app.config file.
 In the same way which I have already described for web application you can read connection from app.config .

But one extra thing you need to do in window application. You need to add reference of System.Configuration in your application.

After that you need to follow step which I have already described for web application.
You can also add common DBConnectionClass.cs class in window application for connection in the same way which I have described in Web section of this article.

Author

Priti Kumari

I am technical blogger.I blogs at www.c-Sharpcorner.com and https://aspdotnetmyblog.blogspot.in/.

No comments:

Post a Comment