This article describes various ways to declare a connection string. We will see the following points.
- Specify Database connection string in Web Application.
- Specify Database connection string in Windows application.
Let’s start from first point.
1. 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:
You need to use the connectionStrings tag to specify the connection string in the web.config file as in the following image.
Write this XML markup in the web config file for declaration of the connection. You can add many tags for each 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 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.
Then write the following 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 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.
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.
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 write 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.
Write the following code in your class:
-
-
-
- public enum Database
- {
- MyTest,
- Northwind
- }
-
-
-
-
-
- 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 a web page.
You can call this method as in
DBConnectionClass.Getconnectionstring(DBConnectionClass.Database.Northwind). 2. Specify Database connection string in Windows applicationNow 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.
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. You need to add a reference of System.Configuration to your application.
Then you need to follow the step 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.