Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Multiple database connection strings in web.config file

Author: Jeff Nava

In this article, we will see how to add multiple database connection strings in web.config file and get those connection strings in code behind file.

 

 

  1. Open Visual Studio 2010
  2. File > New > Website
  3. Visual basic or Visual C# > ASP.NET Empty Web Site
  4. Website > Add New Item > Web Form
  5. Write code below in aspx page

    <asp:Label ID="Label1" runat="server" ></asp:Label>
    <br />
    <br />
    <asp:Label ID="Label2" runat="server" ></asp:Label>
    <br />
    <br />
    <asp:Label ID="Label3" runat="server" ></asp:Label>
    <br />
    <br />
    <asp:Label ID="Label4" runat="server" ></asp:Label>
     
  6. Write code below in web.config file.

    <?xml version="1.0"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
     -->
    <configuration>
           <system.web>
                  <compilation debug="true" targetFramework="4.0"/>
           </system.web>
           <connectionStrings>
                  <add
                         name="NORTHWINDSQLConnection"
                         connectionString="Data Source=localhost;Initial Catalog=NORTHWND;Integrated Security=True"
                         providerName="System.Data.SqlClient"/>
                  <add
                         name="masterSQLConnection"
                         connectionString="Data Source=localhost;Initial Catalog=master;Integrated Security=True"
                         providerName="System.Data.SqlClient"/>
                  <add
                         name="TestExcelConnection"
                         connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Data\Test.xlsx;Extended Properties=Excel 12.0"
                         providerName="System.Data.OleDb"/>
                  <add
                         name="TestMSAccessConnection"
                         connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Data\Test2.accdb;"
                         providerName="System.Data.OleDb"/>
           </connectionStrings>
    </configuration>
     
  7. Now write code below in code behind file to get connection string.

    C#
     
    protected void Page_Load(object sender, EventArgs e) {
        string connectionStringNORTHWIND = ConfigurationManager.ConnectionStrings["NORTHWINDSQLConnection"].ConnectionString;
        SqlConnection sqlConnectionNORTHWIND = new SqlConnection(connectionStringNORTHWIND);
        sqlConnectionNORTHWIND.Open();
        Label1.Text = "Connection to 'NORTHWIND' database in SQL is established successfully";
        sqlConnectionNORTHWIND.Close();
     
        string connectionStringMaster = ConfigurationManager.ConnectionStrings["masterSQLConnection"].ConnectionString;
        SqlConnection sqlConnectionMaster = new SqlConnection(connectionStringMaster);
        sqlConnectionMaster.Open();
        Label2.Text = "Connection to 'master' database in SQL is established successfully";
        sqlConnectionMaster.Close();
     
        string connectionStringExcel = ConfigurationManager.ConnectionStrings["TestExcelConnection"].ConnectionString;
        OleDbConnection excelConnection = new OleDbConnection(connectionStringExcel);
        excelConnection.Open();
        Label3.Text = "Connection to Excel file is established successfully";
        excelConnection.Close();
     
        string connectionStringMSAccess = ConfigurationManager.ConnectionStrings["TestMSAccessConnection"].ConnectionString;
        OleDbConnection MSAccessConnection = new OleDbConnection(connectionStringMSAccess);
        excelConnection.Open();
        Label4.Text = "Connection to MS Access database is established successfully";
        excelConnection.Close();
     
    }
     
    VB.NET
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim connectionStringNORTHWIND As String = ConfigurationManager.ConnectionStrings("NORTHWINDSQLConnection").ConnectionString
        Dim sqlConnectionNORTHWIND As New SqlConnection(connectionStringNORTHWIND)
        sqlConnectionNORTHWIND.Open()
        Label1.Text = "Connection to 'NORTHWIND' database in SQL is established successfully"
        sqlConnectionNORTHWIND.Close()
     
        Dim connectionStringMaster As String = ConfigurationManager.ConnectionStrings("masterSQLConnection").ConnectionString
        Dim sqlConnectionMaster As New SqlConnection(connectionStringMaster)
        sqlConnectionMaster.Open()
        Label2.Text = "Connection to 'master' database in SQL is established successfully"
        sqlConnectionMaster.Close()
     
        Dim connectionStringExcel As String = ConfigurationManager.ConnectionStrings("TestExcelConnection").ConnectionString
        Dim excelConnection As New OleDbConnection(connectionStringExcel)
        excelConnection.Open()
        Label3.Text = "Connection to Excel file is established successfully"
        excelConnection.Close()
     
        Dim connectionStringMSAccess As String = ConfigurationManager.ConnectionStrings("TestMSAccessConnection").ConnectionString
        Dim MSAccessConnection As New OleDbConnection(connectionStringMSAccess)
        excelConnection.Open()
        Label4.Text = "Connection to MS Access database is established successfully"
        excelConnection.Close()
    End Sub

     
  8. Now see the web site in browser

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to bind Repeater Control to Generic List in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net