StrataFrame Forum

Switching database connections

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

By Doug Zapp - 7/9/2009

Is there a way to switch database connections such that I can jump from a development database to a production database, and back again?



I know that I can remove the AppKeys.dat and Connections.dat files to remove the connections and then re-initialize via the Connection Wizard. But this also wipes out all connections rather than a specific one.



Thanks.
By Edhy Rijo - 7/9/2009

Hi Doug,



I use the following routine from a menu option so my users can change the database connection.



Private Sub SetDatabaseConnection()

'-- Check security for this module

If Not CheckSecuritySecurityIsGranted("DatabaseConnection") 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()



End If

End Sub

By Greg McGuffey - 7/9/2009

I do something very similar. The only thing I have added to this is a check to see if the form is dirty, with code to handle that situation.



I allow this only for developers and it allows me to have multiple "dev" environments, as I can setup multiple connections, then just choose which I want. E.g. I have a local connection, which attaches to a local db and I have a remote connection that attaches to a db in our data center.
By Edhy Rijo - 7/9/2009

Greg McGuffey (07/09/2009)
I do something very similar. The only thing I have added to this is a check to see if the form is dirty, with code to handle that situation.


Hi Greg,



I am assuming that when calling form.Close() that if the form is dirty it will ask you if you want to save the changes. I have not tested, but took that assumption for granted Tongue . Can you please elaborate on why are you handling this?
By Doug Zapp - 7/9/2009

So far I like Edhy's approach, which I've modified for my purposes. I like this better because it gives me control ahead of time as to which environment I log into. This code is added in my Program.cs file in ShowLoginAndInitMainForm method. It will only fire if I'm running from VS.



#if (DEBUG)

if (ConnectionManager.ShowAvailableConnectionStrings())

ConnectionManager.SetConnections();

#endif
By Greg McGuffey - 7/9/2009

Edhy,



The only thing I'm adding is how to handle dirty forms automatically (i.e. skip the dialog). I test if it is dirty and then usually just undo (since this is used only the dev environment). Hated clicking cancel 50 times....



Doug,



Glad you got it working! BigGrin
By Edhy Rijo - 7/9/2009

Thanks Greg.



Doug, glad it worked for you too!