Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

NEWBIE: How to programatically alter a ConnectionString?

Posted on 2006-06-01
4
Medium Priority
?
332 Views
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 !

Thanks,
BrianMc1958
0
Comment
Question by:BrianMc1958
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:anyoneis
ID: 16807822
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;
      }
}
}
0
 

Author Comment

by:BrianMc1958
ID: 16807907
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?  
0
 
LVL 11

Accepted Solution

by:
anyoneis earned 1000 total points
ID: 16808505
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.

David

0
 

Author Comment

by:BrianMc1958
ID: 16808869
Great.  I'll give it a try.  Thanks again...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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 lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

569 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