Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting to multiple databases

Status
Not open for further replies.

rsmithinfo

Programmer
Apr 7, 2006
10
GB
I have an SQL server instance and have two databases (this is not avoidable due to the client) I want to be able to use one connection string to access both and then in the queries use:

databasename.dbo.tablename

to access the tables. What connection string will work and do I need to change anything in SQL Server?

Programming in C# by the way.

Thanks a lot in advance.
 
just drop database name from your connection string. If I remember well in C# connection string has Current_Catalog or something, just remove that part of the string.
BTW never try it this in C# so don't expect much. But when I connect to SQL Server with ODBC connection w/o Database=... everything is OK.

Borislav Borissov
 
That didn't seem to work but thanks for responding. My old conn string was:

Server=ROBLAPTOP\\SQLEXPRESS;Database=DatabaseName1;Trusted_Connection=True;

When i was using just the DatabaseName1 databases. It's currently:

Server=ROBLAPTOP\\SQLEXPRESS;Trusted_Connection=True;

But this does not seem to work.
 
just connect to one of the databases and use the databasename.dbo.tablename format. That will work but it is not the best way of handling things. You should open and close connections as fast as possible and then you would just give your connectionpool class the databasename as a parameter. Preferibly using an enum to store the databasenames in.



Christiaan Baes
Belgium

"My new site" - Me
 
Fixed it. Turns out it wasn't the connection string at all so all's good.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top