Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Thursday, 2 April 2015

Introduction:

in this article I will explain how to read connectionstring from app.config file in 
c#, vb.net.  Reference to Namespace System.Configuration is required to read connection string from app.config file in windows or console application in c#, vb.net.

Steps to follow:
1.    create new windows application and open your app.config file and add conneciton string like as shown below
<connectionStrings>
<add name="Con" connectionString="DataSource=localhost;Initial Catalog=test;Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>

2.       Add System.Configuration reference for that follow below steps

Right click on your References folder à select Add Reference à select .NET tab à in that selectSystem.Configuration à Click OK like as shown below


   3.  After adding reference to
System.Configuration, add following namespace on form:

C# Code
using System.Configuration;

VB.NET Code
Imports System.Configuration;

4.    Now  write the following to read connectionstring from app.config file

C# Code
string strcon = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;

VB.NET Code

Dim strcon As String = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;

Categories: , ,

0 comments:

Post a Comment