MS SQL Server
  Home arrow MS SQL Server arrow Page 4 - SQL Connection: Connecting to Multiple Dat...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

SQL Connection: Connecting to Multiple Databases
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 21
    2005-08-22

    Table of Contents:
  • SQL Connection: Connecting to Multiple Databases
  • Saving multiple connections in Web.config
  • Code
  • Connection Using Dynamic Properties of the SQLConnection Control

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    SQL Connection: Connecting to Multiple Databases - Connection Using Dynamic Properties of the SQLConnection Control


    (Page 4 of 4 )

    The information stored in the web.config file can also be used by placing a 'SQLConnection' control from the 'Tools'. Since there are two server connections, two controls (see picture below) should be added to the user interface in the design plane. In the Properties window of the SQLConnection control, you need to configure the DynamicProperties in the Configuration node. By clicking the ConnectionString item of this node, a dialog box pops up asking for 'Mapping' instruction between the "key" in the configuration file and SQLConnection1.ConnectionString.

    After adding the 'SQLConnection' control, the key in the web.config file can be associated with the connection as shown. For example, in the (Dynamic Properties) node,

    the ConnectionString is SQLConnection2.ConnectionString, and its mapping to the web.config stored value is entered by clicking the empty space in the (Advanced) field as shown in the next picture, and picking up the relevant item. Similarly the SQLConnection1 is associated with the 'books.'

    With these settings in place, the code gets much simpler, as shown below.

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As 
    System.EventArgs)_ Handles Button1.Click If DropDownList1.SelectedItem.ToString Is "books" Then SqlConnection1.Open() TextBox1.Text = SqlConnection1.ConnectionString TextBox2.Text = SqlConnection1.State Else If DropDownList1.SelectedItem.ToString Is "info" Then SqlConnection2.Open() TextBox1.Text = SqlConnection2.ConnectionString TextBox2.Text = SqlConnection2.State End If End If End Sub

    The result for one of the choices is shown for completeness.

    Summary

    It is possible to connect any of the SQL 2000 servers whose key/value pairs are stored in the web.config file. It is also possible to use an external file with the same key/value information for the servers. One has to make sure the login for the various databases accessed are in place for successful connection. Using dynamic properties of the SQLConnection the amount of code can be reduced.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This tutorial is a continuation of an article you may find at the following...
       · Once you have your user-chosen connection string, how can you keep it for further...
       · This article is just about keeping multiple connections strings in one place, namely...
     

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT