Net-informations.com
SiteMap  | About    

ASP.NET Connection String

A connection string provides the information that a provider needs to communicate with a particular database. The Connection String includes parameters such as the name of the driver, Server name and Database name , as well as security information such as user name and password.

An ADO.NET Data Provider is a class that can communicate with a specific type of database or data store. Usually Data Providers use a connection string containing a collection of parameters to establish the connection with the database through applications. The .NET Framework provides mainly three data providers, they are

  Microsoft SQL Server
  OLEDB

ODBC

Here you can see how to make a connection string to the following ADO.NET Data Providers.

Microsoft SQL Server Connection String

  connetionString="Data Source=ServerName;Initial Catalog=Databasename;

User ID=UserName;Password=Password"

OLEDB Data Provider Connection String

  connetionString="Provider=Microsoft.Jet.OLEDB.4.0;

Data Source=yourdatabasename.mdb;"

ODBC Connection String

  connetionString="Driver={Microsoft Access Driver (*.mdb)};

DBQ=yourdatabasename.mdb;"

You have to provide the necessary connection information to the Connection String attributes

Web.config and ConnectionString

You can store connection strings in the Web.config file and reference the configuration entries in data source controls. You can create connectionStrings element within the Element element, by create a child element named and place your connection strings there.

  

<configuration>
 <connectionStrings>
   <add name="SQLDbConnection"
		connectionString="Server=SQlServerName; Database=YouDatabaseName; User Id=userid; password= password"
		providerName="System.Data.SqlClient" />
 </connectionStrings>
</configuration>

You can access the connectionstring value at run time in your ASP.NET application as shown in the following example.

vb.net

  Dim conn As String = ConfigurationManager.ConnectionStrings("SQLDbConnection")

C#

  string conn = ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString();

From the following links you can see how each dataprovider makes connection to the database from ASP.NET application.

ASP.NET SQL Server Connection

ASP.NET OLEDB Connection

ASP.NET ODBC Connection




 ASP.NET Data Access - Related Contents