how to connect to multiple database?

C

cipcip

Is it possible to connect to 3 database and execute a cross db query?

I usually use SqlConnection(connectionString) for a single connection
 
F

Frans Bouma [C# MVP]

cipcip said:
Is it possible to connect to 3 database and execute a cross db query?

I usually use SqlConnection(connectionString) for a single connection

Yes. Connecting to sqlserver is a 2-step process. First you connect
to the sqlserver process by the credentials given (either in the
connection string or through windows authentication). That's step 1.
Then you will connect to the catalog you've specified in the connection
string. That's step 2. If you don't have access rights to the catalog
specified, you won't be able to make step2 even if you made step 1.

Once connected to a sqlserver process, you can in theory access every
catalog in the system you have access rights to. So if you're connected
to catalog 'Northwind' and you want to join customers in northwind with
pubs' authors table, you can do something like:

SELECT * FROM northwind.dbo.customers c inner join pubs.dbo.authors a
ON ...

So you have to specify the catalog, schema and table name, not just
the tablename, but that's logical, as otherwise sqlserver thinks the
table is in the catalog you connected to in step2. :)

Frans

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
C

cipcip

i've understood, i have to specify the initialcatalog.owner.table,
but the problem is that i don't know how to connect to 2 different db
at the same time

if i write sql SqlConnection(connectionString1+","+connectionString1),
i get an error

this is the connectionstring:
Data Source=.\SQL............;AttachDbFilename='g:\...help.mdf';Initial
Catalog=help;Integrated Security=True;Asynchronous Processing=True;User
Instance=True
 
J

Jeff Dillon

You don't "connect" to two. Your query refers to the other database. Read
the first post again.

Get it working in Query Analyzer first

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top