Home > DeveloperSection > Articles > Connection Strings

Connection Strings

ASP.Net ASP.Net 
1 Comment(s)
 5593  View(s)
Rate this:

Connection Strings

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.

When you create a Connection object, you need to supply a connection string. The connection string is a series of name/value settings separated by semicolons (;). The order of these settings is not important.  Connection strings vary based on the RDBMS and provider you are using, a few pieces of information are almost always required:

Server Name:Name of the server where the database is located

Database Name:Name of database which you want to use

Authentication:How the database should authenticate you

Connection String for SQL Server

string connectionString = "Data Source=localhost; Initial Catalog=dbABC; Integrated Security=SSPI";


If integrated security isn’t supported then you have to prove valid user id and password combination in your connection string.(User id and password is that password through which you log on to your database)

string connectionString = "Data Source=localhost; Initial Catalog=dbABC; user id=sa; password=pass";


Connection String fro OLE DB

If you’re using the OLE DB provider, your connection string will still be similar, with the addition of a provider setting that identifies the OLE DB driver.

Connection String for Oracle Database

string connectionString = "Data Source=localhost; Initial Catalog=dbABC; user id=sa; password=pass; Provider=MSDAORA";


Here in the example above MSDAORA is name of provider for Oracle database

Connection String for MS Access Database

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Data\dbABC.mdb";


Here, in the above connection string Microsoft.Jet.OLEDB.4.0 is Database Provider for MS Access database, and C:\Data\dbABC.mdb is the path where the database file of MS Access is stored.


For more list of connection string related to different database visit: http://www.connectionstrings.com

For best practice, don’t hard code your connection string for every connection object in your aspx or .cs page, instead write your connection string in web.config’s <configuration> tag, which can make life easy if in future days you want to change your connection string for some reasons, so you can just simply change in one place and you are done. Otherwise, you have to make changes in every page you have mentioned your connection string.

Here’s an example:




       <add name="dbConnectionString"connectionString="Data Source=localhost; Initial   Catalog=dbABC; user id=sa; password=pass" providerName="System.Data.SqlClient"/>





You can then retrieve your connection string by name of the connection string (here we have used dbConnectionString).


string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString "];


If you want to provide connection string for Data source on aspx page directly then you can use <%$ ConnectionStrings:dbConnectionString %> for its connection string property.


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnectionString %>" SelectCommand="SELECT * FROM Table1">


Becoming More Genius aah???

By mohan kumar on   5 years ago
Thanks for sharing wonderful resources..

Don't want to miss updates? Please click the below button!

Follow MindStick