NEWBIE: How to programatically alter a ConnectionString?

Posted on 2006-06-01
Last Modified: 2010-04-16
Dear Experts,

I'm a total newbie at C# and VS2005, coming from Java world...

I have made my first useful in-house app, which uses a DataGridView to allow access to a particular table.  Next, I'd like give the user the ability to point to the "same" table in a different database (on the same box).  (We typically have multiple versions of the "same" db...)  

This can be crude and simple.  It would be OK for now to hard-code a drop-down list, for instance, and allow the user to select "Database1, Database2...".  The app would start up as now, and default to Database1.

I know this probably involves altering the ConnectionString.  But:

1.  It's in XML, which I haven't worked with much.  Would I need to programatically alter that string?

2.  And I don't understand the mechanics involved.  Assuming I altered that string, then what do I do?  Would I need to restart basically the whole app?  

3.  I'm assuming I can switch databases at run-time, right?  (Given that the table definitions themselves are identical)

Any general help would be appreciated !

Question by:BrianMc1958
    LVL 11

    Expert Comment

    Are you using a strongly typed dataset? If you are using a data layer dll, you will need to add a class to the data layer, such as:

    public class tableNameConnectionManager
       public static TableNameTableAdapterConnectionManager
          public static SqlConnection GetConnection(TableNameTableAdapter adapter)
             return adapter.Connection;
          public static void SetConnection(TableNameTableAdapter adapter, SqlConnection  connection)
             adapter.Connection = connection;

    Author Comment

    Dear anyoneis,

    This is just a toy app, for learning, at the moment, because I am an idiot at the moment.  I'm just trying to throw together a quickie UI, and I don't know if I'm using a strongly typed dataset or not.

    Your response is similar to a factory method I had previously received from EE, regarding switching between different TYPES of databases (MS SQL, Oracle...)  

    Could you say a little more about how I would get from Database1 to Database2 using your example?  How does the actual XML fit into your response?  (I'm trying to avoid actually editing XML to solve this problem...)  Is the SqlConnection the same thing as the XML ConnectionString?  
    LVL 11

    Accepted Solution

    The SqlConnection object gives you access to a ConnectionString property. There really is no XML involved, you just close the current connection, change the connection string (which has the database identified in it) , and reopen the connection.



    Author Comment

    Great.  I'll give it a try.  Thanks again...

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 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

    22 Experts available now in Live!

    Get 1:1 Help Now