StrataFrame Forum

Programmically Connecting to Multiple Databases using Same BOs

http://forum.strataframe.net/Topic30066.aspx

By Jeff Pagley - 6/15/2011

Hi SF Team,

I have an unusual challenge I need some help on.  I will be creating new databases with thesame schema on a regular basis on the same server.  Therefore, I will not only have existingdatabases, but I will constantly have new ones. Within one single application I need to be able to randomly select  any four of these databases and in some wayuse the same BO classes to connect and CRUD to each database.  Any suggestions on how I might be able to dothis? 

Thanks,

Jeff 

By Edhy Rijo - 6/15/2011

Hi Jeff,

If you need to connect to this different database one at a time, you can create as many connections as you want with the SF Connection Manager and simply change the connection to the one you want to work with.  I do this all the time to remotely connect to my customers data to generate reports or test some new functionality with current data.

To go this route, simply call the connection manager from a menu item or somewhere else you want to be able to select from your connections, here is the vb code I use just for that, feel free to adjust it to suit your needs.


Private Sub SetDatabaseConnection()       
        '-- Check security for this module       
        If Not Basics.HasPermission("Database Connection"Then
                 Exit Sub
        End
 If
        '-- Show the connection dialog and allow a connection to be selected
         If MicroFour.StrataFrame.Data.ConnectionManager.ShowAvailableConnectionStrings() Then
             '-- Since a connection was selected, then all of the existing dialogs need to be closed
             '   since their connection is established to the original source.
            For Each loForm As Form In Me.MdiChildren
                 loForm.Close()
                 loForm.Dispose()
             Next
             '-- Force the connections to be reset
             MicroFour.StrataFrame.Data.ConnectionManager.SetConnections()

             '-- Set the Connection label on the status bar
             Me.RibbonControl.Items("sbConnectedToItem").Caption = "Connected to: " & GetApplicationActiveConnectionStringTitle(ConnectionManager.ApplicationKey, "")
         End If
 End Sub

By Jeff Pagley - 6/16/2011

Thanks Edhy for the reply.  I use the Connection Manager now. But I need to clarify what I want to do.  I need to have 4 connectionsopen at the same time to 4 different databases within the same application andthen have the BOs use those 4 connections for CRUD operations.  Is this possible?

Jeff

By Edhy Rijo - 6/16/2011

Jeff Pagley (6/16/2011)
Thanks Edhy for the reply.  I use the Connection Manager now. But I need to clarify what I want to do.  I need to have 4 connectionsopen at the same time to 4 different databases within the same application andthen have the BOs use those 4 connections for CRUD operations.  Is this possible?

Yes, you can have as many connections open as you want.  In the AppMain.vb or Program.cs add a data source item in the SetDataSources method for each database.  Make sure you fill the DataSourceKey parameter for each one, then you can set the BO.DataSourceKey to use the connection you want to get the data from.
By Jeff Pagley - 6/16/2011

Thanks for the help!

Jeff