tags:

views:

37

answers:

4

Hi,

With linq to sql, where is the database connection information stored?

How could I override the database to another database on a per query basis?

A: 

The database connection for linq is in the web.config or application.config files.

aleemb
+3  A: 

You can't do it per-query; but you can per-data-context. Just pass in a different connection or connection-string to the constructor:

string connectionStringA = ..., connectionStringB = ...
using(var ctxA = new FooContext(connectionStringA)) {...}
...
using(var ctxB = new FooContext(connectionStringB)) {...}

using(SqlConnection conn = ...)
using(var ctxC = new FooContext(conn)) {...}
Marc Gravell
A: 

You pass it to the DataContext instance.

With a generated DataContext subclass, the default constructor will use project .Settings, and thus from the .config file.

Richard
A: 

As aleemb said, the database information is stored in the config files. Check the one in the project where you created your dbml map.

That said - the DatabaseContext has a constructor that takes the connection string as a parameter. However, I'm not sure there's a good way to override that on a per query basis without creating a new DatabaseContext object. Which really could cause you issues in the future if you're creating entities from two different DatabaseContext objects.

John Christensen