NEWBIE: How to change a ConnectionString?

Posted on 2006-06-02
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

    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

    If you are using .NET 2 then you would be interested in this article
    LVL 12

    Assisted Solution

    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

    Thanks, folks.  That's all helpful.

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


    LVL 25

    Expert Comment

    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:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now