Change Databases Dynamically at Run-Time

In some situations, you may need to connect to a different database based on the currently logged in user. This can be accomplished by dynamically changing connection strings.

March 8, 2006
Iron Speed Designer V3.2

Procedure
In some situations, you may need to connect to a different database based on the currently logged in user. For example, say there are four identical databases and you require the ability to connect to one of the four based on the logged in user. This can be accomplished by dynamically changing connection strings. You can define multiple connection strings, one for each database, and then change the default connection string used by a database table dynamically. Here are step-by-step instructions on how to do this.

Step 1: Create an application using one of the identical databases. The Web.config file will contain a connection name key to connect to the first database including the type of connection, user Id and password as follows:

<add key="DatabaseNorthwind1" value="Provider=SQLOLEDB;
      Data Source=(local);
      Database=Northwind1;
      Trusted_Connection=yes;
      User Id=;
      Password="
/>

Step 2: Modify your application’s Web.config file to create connection name keys for each database, e.g:

<add key="DatabaseNorthwind1" value="Provider=SQLOLEDB;
      Data Source=(local);
      Database=Northwind1;
      Trusted_Connection=yes;
      User Id=;
      Password="
/>

<add key="DatabaseNorthwind2" value="Provider=SQLOLEDB;
      Data Source=(local);
      Database=Northwind2;
      Trusted_Connection=yes;
      User Id=;
      Password="
/>

<add key="DatabaseNorthwind3" value="Provider=SQLOLEDB;
      Data Source=(local);
      Database=Northwind3;
      Trusted_Connection=yes;
      User Id=;
      Password="
/>

<add key="DatabaseNorthwind4" value="Provider=SQLOLEDB;
      Data Source=(local);
      Database=Northwind4;
      Trusted_Connection=yes;
      User Id=;
      Password="
/>

Step 3: For each database table that you would like to change, override the ConnectionName property for the SqlTable class. For example, if you have a Customers table, you will have a CustomersSqlTable class in:

MyApp\DataAccess\CustomersSqlTable.vb

You can retrieve the userName of the currently logged in user from the session data and compare this user name to return the connection name.

Public Overrides Property ConnectionName() As String
      ' Define the Get property
      Get
      ' Get the user name from the session
      Dim sessionData As System.Collections.Hashtable

      sessionData = CType(System.Web.HttpContext.Current.Session.Item( _
            BaseClasses.Utils.SessionConst.LoginInfo), _
            Hashtable)

      Dim userName As String
      userName = CStr(sessionData(BaseClasses.Utils.SessionConst.UserName))
      ' Select the connection name key based on the user
      Select Case username
        Case "User1"
         Return "DatabaseNorthwind1"
        Case "User2"
         Return "DatabaseNorthwind2"
        Case "User3"
         Return "DatabaseNorthwind3"
        Case "User4"
         Return "DatabaseNorthwind4"
      End Select

      ' Default case
      Return MyBase.ConnectionName()
    End Get

     ' Define the set property
     Set(ByVal Value As String)
       ' call the underlying base set method.
       MyBase.ConnectionName = Value
    End Set
End Property

Step 4: Repeat this customization for all tables that require run-time switching. To make this change across your application, modify the base class where all SqlTable classes are derived from in the BaseClasses. The base class for each SqlClass is DynamicSQLServerAdapter. This is defined in:

BaseClasses\Data\SqlProvider\DynamicSQLServerAdapter.vb

Step 5: Build and Run your application.

About the Author
Alan S. Fisher
Co-Founder and Chairman of Iron Speed, Inc.

Mr. Fisher was a General Partner at Outlook Ventures, Inc., a venture capital company prior to co-founding Iron Speed, Inc. He co-founded Onsale, Inc. (now part of Amazon) and was its Chief Technology Officer from July 1994 to December 1999. He also co-founded and was President of Software Partners, Inc, a developer and publisher of software products from August 1988 to July 1994. From April 1984 to August 1988, Mr. Fisher served as Technical Marketing Manager and Product Development Manager for Teknowledge, Inc., a developer of artificial intelligence software products. From June 1981 to April 1984, he served as a member of the technical staff for AT&T Bell Laboratories.

Mr. Fisher has served on the boards of several publicly traded companies including Onsale, Inc. (NASDAQ:ONSL), an Internet auction retailer; Egghead.com (NASDAQ:EGGS), an Internet retailer of computers and software; FatBrain, Inc. (NASDAQ:FATB), an Internet retailer of technical and professional books; and Infodata Systems Inc. (NASDAQ:INFD), an e-business consulting services company.

Mr. Fisher received his B.S. in Electrical Engineering from the University of Missouri and received his M.S. in Electrical Engineering from Stanford University.



  Privacy Statement