Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am working on vb.net project with two different sql databases with one connection. Application consists of two sections which is section1 and section2. If I change section2 data should save on database2 or else data should save on database1. So I need help with connection class and section.

Sections change with pull down menu.

Hope will get help from you. Thank you in advance.
Posted
Updated 10-May-15 9:11am
v2
Comments
Sascha Lefèvre 10-May-15 14:12pm    
What's the problem, exactly?
PIEBALDconsult 10-May-15 14:40pm    
What database system? Are both databases on the same server?

If you are talking about a connection string, then you cannot connect to two databases with one connection string. A connection string is made up of following things.


  1. Server name -- The database server
  2. Database name -- The name of the database you're connecting to
  3. ... Other authentication settings


This makes up your connection string. So, once you will enter a name of database you will ensure that you want to connect to a particular database instance. How would this connect to two separate databases? It cannot...

However... There is another turn around, in which even if you are connected to a single instance. You can still connect to other databases, think of the following line,

SQL
[DbName].dbo.[TableName]


It uses full name for a database and table. Now when you will write this object in your SQL statement. It would not simply just check inside your own database (that you are connected to using that connection string) but instead it would ask your server to return the response for that database's table. So this can be usable if you want to use other database instances while using connection string for one database. But this does not mean that you are now connected to other databases also.

So in your sections, you can maintain a single object for retrieving the data. This is why using a Model is always considered good practice (IMO). Your model would contain all of the connections, data types and other objects for your database (Ever worked with Entity framework?). Which you can then use by calling instance or static members of this "Model" class.
 
Share this answer
 
v2
Comments
Maciej Los 11-May-15 2:03am    
5ed!
Afzaal Ahmad Zeeshan 11-May-15 3:40am    
Thank you Maciej.
Directly saying, it's impossible. Afzaal Ahmad Zeeshan[^] is right (see solution 1).

But... You can always change database (on the same server) even if you're using one connection string. It's quite easy with SqlConnection.ChangeDatabase method[^] ;)

Another option is to use SQL command like this:
SQL
USE DataBase2;
SELECT ...
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 11-May-15 3:40am    
Dang, I did miss this USE Database2; part. +5 for you too Maciej. :)
Maciej Los 11-May-15 3:54am    
Thank you, Afzaal ;)
Mohamed Anees 11-May-15 6:44am    
One application with multiple database
Maciej Los 11-May-15 8:53am    
And...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900