Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

Visual Studio 2012 VB.NET - Change SQL connection dynamically

I have an application that is connected to two SQL Server databases, however I dynamically need to change one of the SQL connections to other databases on the fly.  The databases have exactly the same database scheme and are located on the same SQL Server.

It appears that most of the database connections are read-only and can't be changed.  Is it possible to update this?
0
Cmitch
Asked:
Cmitch
  • 3
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
why can't you create new SqlConnection object?

Dim SQLConn As SqlConnection = New SqlConnection
SQLConn.ConnectionString = "Data Source=servername;" & _
"Initial Catalog=databasename;" & _
"User ID=username;" & _
"Password=userpassword;"

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
can u post the current connection code u use?
0
 
CmitchAuthor Commented:
I can successfully create an SQLconnection, however how can I assign this to the exist DataSet and associated TableAdapter?
0
 
Meir RivkinFull stack Software EngineerCommented:
u modify the adapter, for example:
theAdapter.Connection.ConnectionString = connectionString;

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
you cannot change the connection of currently opened objects. If you change your connection, you need to create new objects
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now