Question Switching databases, keep dataset

jamie123

Well-known member
Joined
May 30, 2008
Messages
82
Programming Experience
Beginner
I was reading one of cjard's posts http://www.vbdotnetforums.com/showthread.php?t=16425&highlight=Application+scope about switching databases between live, test, and dev. I recently found out that I have to change databases...sort of, the database I am switching to has all of the same tables, it is only missing about 1 or 2 which I plan on adding .but aside from that all the same. In that thread that I pasted, it seems as if I change the connection string through that way, I cannot modify the dataset designer..which I will need to be doing since I am permanently switching databases. What can I do to permanently switch databases, and have all of the function as if it was the same database that I created the dataset with?

Thank you!
 
Well, you have a lot of work to do becuase:

You can only programmatically change the ConnectionString setting at runtime if it is NOT ApplicationScope/Connectionstring. It MUST be User/string type if you want to edit it

Every time the dataset changes, the TableAdapters are regenerated, and the default code goes and picks up from My.Settings so changes to this will be lost if you modify it to pick up a different value

To change the dataset itself, the ConnectionString MUST be AppScope/ConnStr type because the designer actually uses it to connect to the database..

-

The way I see it, you either:

Remember to change it all the time: When you want to edit the dataset, make it App/Conn, and in production you make it User/String

Investigate the use of compile time conditionals:
VB.NET:
Expand Collapse Copy
#IF DEBUG
  This code will be put in debug
#ELSE
  This code will be put in Release
#END IF
to make the debug connstr always AppScope/ConnStr and the release connstr (and relevant assignment) always User/String

Set the ConnectionModifier to Public for one of your tableadapters (the change propagates to all) and then remember to set it every time in every form. You could get really smart and write a form from which all your forms inherit, that searches itself for any tableadapters (using reflection) and sets the connectionstring of them.. The only issue is it will be using reflection, because by default TableAdapters inherit from Component, which is a useless thing to inherit from; it allows no sharing of common features of a tableadapter, and is a severe oversight by microsoft in my opinion


Modify the autogenerated code of My.Settings so that any calls to get the conenctionstring actually result in a different string being returned depending on what mode the app is operating in
-

Simply put, MS didnt envisage changing databases and I can actually see perhaps the final solution as being the simplest, as your code changes shouldnt be wiped unless you add new settings.
You might additionally have some success writing a macro in Visual Studio to do some of these things for you, but I have never bothered, and usually just choose the route that I first discovered of being diligent about altering the Settings between AppScope/ConnStr and User/String
 
Back
Top