NEWBIE: How to change a ConnectionString?

Posted on 2006-06-02
Medium Priority
Last Modified: 2012-05-05
Dear Experts,

I have an existing ConnectionString, and I want to change the database it points to.  Is there a clean way to do that in C# without actually parsing the String manually?  Like, can the "database" part of the String be accessed like it was an Object in it's own right?

I currently have:


With this value:

  Data Source=myMachine;Initial Catalog=myDatabase;Persist Security Info=True;User     ID=myUserID;Password=myPassword

Is there any kind of method like:

  Connection.ConnectionString.setInitialCatalog("MyOtherDatabase") ?

If not, what does everybody do?

Question by:BrianMc1958
LVL 25

Accepted Solution

dstanley9 earned 400 total points
ID: 16820267
There is not a clean way that I know of without manipulating the string.
The string determines the connection properties, not vice-versa.

You could store the connection string as a template, ans just add in the database usie string.Format:

string connTemplate = "Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID=myUserID;Password=myPassword";

string connString = string.Format(connTemplate,"myMachine","myDatabase");

I don't know about "everybody", but I have never had a need to programatically change just one part of a connection string.  I will either keep connection strings in config files (or Settings in VS 2005) or use a utility to get conn strings dynamically:


LVL 35

Assisted Solution

mrichmon earned 300 total points
ID: 16821324
If you are using .NET 2 then you would be interested in this article

LVL 12

Assisted Solution

gbzhhu earned 300 total points
ID: 16827786
If I understand you right you could have your app.config or web.config contain the 2 connection strings then use
        ConfigurationSettings.AppSettings["Connectionstring1"] ;

unless you have unknown number of connectionstrings this should work fine.  you can also change config file modify connectionstrings and you don't have to recompile your app

Author Comment

ID: 16833435
Thanks, folks.  That's all helpful.

I should mention that for my very narrow purpose, I also found this:


LVL 25

Expert Comment

ID: 16833502
Interesting.  That's new to .Net 2.0.

I don't know if it makes any difference to you, but I imagine it uses the same connection, just issues a "USE {database name}" command.  The connection must still be established using the original connection string.
However, you could try not supplying an initial catalog (which will use the default database for the user) and change the database at run-time.

Also, keep in mind that any open DataReaders must be closed before changing databases:


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question