Getting Sql Connection Error Again and Again After Commenting the Connection String Asp net Error
This article describes various ways to declare a connection string. We volition see the following points.
- Specify Database connection string in Web Awarding.
- Specify Database connection string in Windows application.
Let's beginning from the first point.
Specify Database connection string in Web Application
In a spider web application we can specify a database connection cord in one of the following two ways.
- Specify it in the web.config file.
- Create a common course file for the connexion string.
Write connection string in web.config file
The benefit of writing a connexion string in a web config file is there is no need to write code every fourth dimension to connect to the database. It reduces the redundancy of lawmaking over a single application. One more benefit of this is whenever we need to alter the database name or credentials nosotros need to change simply the spider web config file.
Now I will describe how to specify a connection string in aweb.config file.
Get to the Web.config file equally in the following:
You need to use theconnectionStrings tag to specify the connection string in theweb.config file equally in the post-obit image.
Write this XML markup in the web config file for declaration of the connection. You tin can add many tags for each database connection.
- <connectionStrings>
- <add name="mytest" connectionString="Information Source=.\SQLEXPRESS;Initial Catalog=mytest;Integrated Security=True;" providerName="System.Data.SqlClient" />
- <add proper noun="Northwind" connectionString="Information Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;" providerName="System.Data.SqlClient" />
- <add proper name="Northwind" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=databasename;uid=sa;pwd=sa;" providerName="System.Data.SqlClient" />
- </connectionStrings>
Read Connection cord from Web.config file
Now I will draw how to read a web.config connection string in an ASP.Net web awarding.
- You lot need to add together grade files to an ASP.Net web application.
- Then you need to include a namespace using Arrangement.Configuration in the class file.
Then write the post-obit method in this form.
- public static string Getconnectionstring(cord keyname)
- {
- string connection = cord.Empty;
- switch (keyname)
- {
- example "mytest":
- connection = ConfigurationManager.ConnectionStrings["mytest"].ConnectionString;
- break;
- example "Northwind":
- connection = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
- interruption;
- default:
- break;
- }
- return connexion;
- }
In the preceding I take written a method to get the connectedness on the basis of the keyname passed.
And so I will show you how to use the preceding method of this class in code to access connections. In the following code yous can run across 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 connectedness string from a web.config file then become to this link:Click hither to set up the following error.
Using the preceding described way you can connect to the database using this method, there is no demand to write a connection code in every page.
Now nosotros volition see the next way to specify a connection.
At present I will create a common class file with connection code.
I take created a DBConnectionClass.cs file and then written the post-obit code in this course. You lot can utilize this class file in multiple Spider web applications where you need to use the same database connection.
In this class I alleged an enum for the list of the database. We tin specify all the database names to be used across application and access them using an enum.
And then I wrote a method, GetConnectionstring, with Database enum parameter.
Write the following lawmaking in your course:
- /// <summary>
- /// listing 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=zilch;
- switch (DBName)
- {
- case Database.MyTest:
- connection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=mytest;Integrated Security=True");
- break;
- instance Database.Northwind:
- connection = new SqlConnection("Information Source=.\\SQLEXPRESS;Initial Itemize=Northwind;Integrated Security=True");
- break;
- default:
- intermission;
- }
- render connection;
- }
Then I volition describe how to access this form method in a web page.
You tin can call this method as in DBConnectionClass.Getconnectionstring(DBConnectionClass.Database.Northwind).
Specify Database connection string in Windows application
Now I volition depict how to declare a connection string in an App.config file of a Windows application.
In the same fashion you saw in the web.config file you demand to write this tag inside the app.config file.
In the aforementioned way I already described, for the spider web awarding you can read a connection from the app.config.
But one extra matter you need to practise in Windows applications is that you demand to add together a reference of System.Configuration to your application.
Then you need to follow the steps I already described for spider web applications.
You tin likewise add a mutual DBConnectionClass course in the Windows application for the connexion in the same mode that I described in the Spider web section of this article.
Source: https://www.c-sharpcorner.com/UploadFile/7d3362/various-ways-to-specify-connection-string-in-Asp-Net-web-app/
0 Response to "Getting Sql Connection Error Again and Again After Commenting the Connection String Asp net Error"
Post a Comment