Accesing multiple database using same connection in sql

suppose you want to connect to database DB1 and DB2 in a join statement where connectionstring is same,it can be done
as follows-

Method1
connectionstring-Data Source=P06-INF-WS-1\SQLEXPRESS;User ID=sa;Password=sa
select * from DB1.dbo.Table1 tb1 join on DB2.dbo.Table2 tb1 where
tb1.id=tb2.id
Method2
Data Source=P06-INF-WS-1\SQLEXPRESS;Database=DB1;User ID=sa;Password=sa
select * from Table1 tb1 join on DB2.dbo.Table2 tb1 where
tb1.id=tb2.id
Table1 is assumed to be in DB1 as its specified in the connection string

1 comment: