Link to home
Start Free TrialLog in
Avatar of BrianMc1958
BrianMc1958

asked on

NEWBIE: How to programatically alter a ConnectionString?

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 !

Thanks,
BrianMc1958
Avatar of anyoneis
anyoneis
Flag of United States of America image

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;
      }
}
}
Avatar of BrianMc1958
BrianMc1958

ASKER

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?  
ASKER CERTIFIED SOLUTION
Avatar of anyoneis
anyoneis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great.  I'll give it a try.  Thanks again...