Multiple Forms Using One OleDBConnection?

Samueul

New member
Joined
Nov 3, 2005
Messages
2
Programming Experience
Beginner
I have an app with several forms. Each form has an OLE connection to the same MS Access DB. I currently have an oledbconnection, oledbdataadapter, and a dataset created on each form via toolbox.

I have a development and production version of the app and everytime I want to switch between the two, I have to go in and edit the connection of the oledbconnection object on each form, I would like to be able to change the connection string from one place to affect the entire app.

I know I can declare a public connection in a module etc.. and it would work, but when I do, and then go back to the oledbdataadapter created via toolbox, I cannot select the connection, I only get none or the existing oledbdataadapter that I created via toolbox. Is there a way to do this so I only have to change my connection string in the publicly declared variable?

I hope that made sense and thank you all for your help.
 
Unfortunately, one of the downfalls of using form view controls is that it's not as dynamic. If you want to stay on that course, the best thing you can do is like you said; use a global variable. Then, on each form load, set the connection string of the control to the global before the connection is made.

My suggestion is not to use them. If you build your own connection component, and reference that; you're still able to hide the connection string and have a single point to manage all of the forms. Not to mention, you can keep the connection string private to the component the forms are referencing, instead of broadcasting it to anything that asks for it.
 
You can have a single, global variable that all the individual connections get their connection string from. Also, you don't have to keep changing the connection string because VS.NET supports conditional compilation:
VB.NET:
#If DEBUG Then
    Public Const myConnectionString As String = "Connection string for debug build"
#Else
    Public Const myConnectionString As String = "Connection string for release build"
#End If
Then the correct code will be compiled whether you are debugging or not. If you put this code in a module and then add code to each form to get the connection string from the module. An alternative would be to use the config file and have every connection use the same value. ConnectionString is a dynamic property so all you need is a few mouse clicks in the properties window, but make sure you set all connections to use the same named value, or else you'll end up with an entry fro each one. For more on dynamic properties: http://msdn.microsoft.com/library/d...spreservepropertysettingsinvisualbasicnet.asp
or do a help/MSDN search for more information.
 
Back
Top