Overriding Dataset Settings Connection String

| 1 Comment | No TrackBacks
So say you have a database connection [DataSet] that you have added to your project.  You do not want to store the password with it (which will end up in the .config file) and may want to change the database name at runtime from what you are using at runtime.  Doing this seems a bit off, since you can not write to the ConnectionString that is creating in the settings portion of the VS designer!  You can not change it to type User instead of Application scope either.

A useful tidbit that I discovered when looking for how to change this.  While it should seem immediately obvious how to modify a connection string at runtime, it is not.  After doing some digging I found the following code to place in Settings.cs [Go to settings, then click "View Code"].

In Settings.cs:
Now when working with your dataset, each time it needs to get the connection string it will return your custom connection string.  This is useful incase you do not want to store the password in your configuration files.  Also, this will allow switching different databases of the same type with your dataset at runtime.  I do that -- I have a "master" database that I use for development, and at runtime I switch to the live database.. both have exactly the same type of structure.

Hope you find this useful!

Matthew MacSuga

No TrackBacks

TrackBack URL: http://www.csharpbydesign.com/cgi-bin/mt/mt-tb.cgi/13

1 Comment

Hi, good day.

Thanks for the information you have here.
It solved the problem I have when deploying and connecting to a production database.

Keep up the good work.

Jeff

Leave a comment

About this Entry

This page contains a single entry by Matthew M. published on January 28, 2008 7:44 PM.

My First Post - A C# Plugin Paradise was the previous entry in this blog.

Generic C# Object Copy Function is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.