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.

  1. Specify Database connection string in Web Awarding.
  2. 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:

specify connection string

You need to use theconnectionStrings tag to specify the connection string in theweb.config file equally in the post-obit image.

connection string

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

  1. <connectionStrings>
  2.    <add name="mytest" connectionString="Information Source=.\SQLEXPRESS;Initial Catalog=mytest;Integrated Security=True;" providerName="System.Data.SqlClient" />
  3.    <add proper noun="Northwind" connectionString="Information Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;" providerName="System.Data.SqlClient" />
  4.    <add proper name="Northwind" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=databasename;uid=sa;pwd=sa;" providerName="System.Data.SqlClient" />
  5. </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.

    namespace

Then write the post-obit method in this form.

Configuration

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

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.

method

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.

erroe

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.

code

Write the following lawmaking in your course:

  1. /// <summary>
  2.  /// listing 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=zilch;
  17.      switch (DBName)
  18.      {
  19.          case Database.MyTest:
  20.              connection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=mytest;Integrated Security=True");
  21.              break;
  22.          instance Database.Northwind:
  23.              connection = new SqlConnection("Information Source=.\\SQLEXPRESS;Initial Itemize=Northwind;Integrated Security=True");
  24.              break;
  25.          default:
  26.              intermission;
  27.      }
  28.      render connection;
  29.  }

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).

GetConnectionstring

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.

DBConnectionClass

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.

app config

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.

giffordight1942.blogspot.com

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel