Connection String in Asp.net C# Web Form

Connection String in Asp.net C# Web Form

Connection Strings are needed to establish a connection with a database. You can use multiple connection strings in an application.
In Asp.net Applications, it a good practice to place your connection strings in web.config file. Apart from that you can also can place your connection string in every .cs file wherever you need to establish a connection. To write connection string separately in all files makes it hard to make changes.

Web.config:

Configuration file is a file that provides Information about the configuration that you can change without having to recompile code.
Web.config file, as it sounds like is a configuration file for the Asp .net web application. An Asp .net application has one web.config file which keeps the configurations required for that application. Web.config file is written in XML with specific tags having specific meanings.

Now to write a connection string you need to follow the steps below:

1. Open your Web.config file.

2. Connection strings are written inside the <Configuration> tag, before the <system.web> tag. A sample connection string looks like the following:
<connectionStrings>
<add name="ConnectionStringName" connectionString="Data Source= (Default); Database=DatabaseName; User ID=Username; Password=Your Password" providerName="System.Data.SqlClient"/>
</connectionStrings>

You can have multiple connection strings by using add tag for different connections.

Asp.net Beginner, Coding for beginners, Coding tutorial, Computer Science, Learn Coding, Learn programming, Programming for beginners, Programming Tutorial, Visual Studio

Following are the parts of an add tag:

Name:

You can place any name of your choice that will be used later for accessing the connection string. e.g. name="myConn"

Data Source:

Open your SQL Server Management Studio, and copy your Server name and paste it against the data source. e.g. Data Source=LENOVO-PC. .; is used for default data source.

Database:

Create a new database for your new application and write down its name against Database. e.g.
database=myDatabase

Authentication:

If you have installed your SQL Server with windows authentication, instead of writing username and password you can only write
integrated security=SSPI

Sample connection string:

    <add name="DBCS" connectionString="data source=.; database=testApplication; integrated security=SSPI" providerName="System.Data.SqlClient" />

If you have installed your SQL Server with SQL Server Authentication mode, write down the User Id and Password for your SQL Server.
uid=Username; Password=MyPassword

Sample Connection String:

<add name="ConnectionStringName" connectionString="Data Source= (Default); Database=DatabaseName; uid=Username; password=MyPassword" providerName="System.Data.SqlClient"/>

All parts of the connection string are separated by semicolon.

providerName:

It will be the same if your are always working with SQL Server, it will be different if you are working with other database like oracle, mySql etc.

Asp.net Beginner, Coding for beginners, Coding tutorial, Computer Science, Learn Coding, Learn programming, Programming for beginners, Programming Tutorial, Visual Studio


Let me know in the comment section if you have any question.

Previous Post:
List Controls in Asp.net C# Web Form

Coming Soon:
SqlDataSource in Asp.net C# Web Form

Comments