Overriding Dataset Settings Connection String

| 3 Comments | 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


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.


Hi all,

this is ONE way to do it (if you prefer the project settings).

But remember that you also can set the connection string WITHIN the data access controls.

E.g. setting the connection string in the "table adapter" objects at runtime (refer to connection property for further details).

Every method has its advantages and disadvantages!

Best regards


Almost a couple years later and this little tidbit is still really useful. It seems like this solution kind of wipes out the possibility of using the configuration file as intended.

Is that the case or am I missing something? Would I have to capture every single property by name or is there a way to make this thing pass through any uncaught propertyName to the default behaviour?

For Visual Studio 2008, this is what I ended up with to get it working:

public override object this[string
if (propertyName == "ConnectionString")
return Utility.ConnectionString;
return String.Empty;

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.