Store connection string in web.config

Posted by Bagus Swara On 11:35 PM

Store connection string in web.config

Assalamualaikum wr.wb
It is very easy to store the connection string in a config file and there are several benefits in doing so. This article describes why and how to store the connection string in web.config.

Introduction
It is a good practice to store the connection string for your application in a config file rather than as a hard coded string in your code. The way to do this differs between .NET 2.0 and .NET 3.5 (and above). This article cover booth.

Connection string in .NET 2.0 config file


In the appSettings location, add a key named whatever you like to reference your connection string to.

<appSettings>
 <add key="myConnectionString" value="server=localhost;database=myDb;uid=myUser;password=myPass;" /> 
</appSettings>


To read the connection string from code, use the ConfigurationSettings class.

string connStr = ConfigurationSettings.AppSettings("myConnectionString");

Now you have the connection string loaded from web.config into your string variable in code.

Connection string in .NET 3.5 (and above) config file


After open web.config file in application and add sample db connection in connectionStrings section like this


<connectionStrings>
 <add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System.Data.SqlClient"/> </connectionStrings >


Contoh Penggunaanya
<connectionStrings> 
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB" providerName="System.Data.SqlClient"/> 
</connectionStrings >

To read the connection string into your code, use the ConfigurationManager class.

VB.NET
Imports System.Data.SqlClient
Imports System.Configuration
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'Get connection string from web.config file
Dim strcon As String = ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString
'create new sqlconnection and connection to database by using connection string from web.config file
Dim con As New SqlConnection(strcon)
con.Open()
End Sub
End Class

C# code
using System;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{   
//Get connection string from web.config file
string strcon = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
//create new sqlconnection and connection to database by using connection string from web.config file
SqlConnection con = new SqlConnection(strcon);
con.Open();
}
}

Remember to add reference to the System.Configuration component. Then include the namespace ("using System.Configuration;" in C# and "imports System.Configuration" in VB.Net) in your code to get access to the ConfigurationManager class.

Summary


Always store the connection string in a config file. It's not any harder once you get used to it and you will benefit from it as it is much easier to change connection string properties when your application is in production.

Post a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( :-q =))

Silahkan dishare dan tinggalkan komentar untuk kemajuan blog ini.
Terima kasih atas komentarnya brada.....!

Bagus Swara

Blog ini ane buat dengan tujuan sabagai E-Book electronic jadi siapa saja yang membutuhan artikel yang ada di blog ini silakan di digunkan, semoga apa yang ane sajikan dapat bermanfaat bagi ente semua....Read More

Langganan

Masukan Email Ente:

Postingan terbaru

Koment Terbaru

Followers