views:

156

answers:

2

I need to fetch records from tables, that are in two diff. databases in two different SQL Servers.

For Instance. Sales DB on server1 and Purchase DB on server2. Both Sales and Purchase DB's have some set of tables say table1 in Sales DB and table2 in Purchase DB. Now, I need to get records from table1 and table2 that are having some common records by joining them.

Using T-SQL i can do it by linking the servers and then quering them.

Please suggest, how can i do it using LINQ to SQL as am'nt aware of it.

Thanks.

A: 

There are (at least) two possible solutions for this.

You could define a stored procedure that does the cross-database query with which you are already familiar. Add the stored procedure as a method on your data context.

Or, you could define a data context for each database, then select the combination in Linq. Something like:

var table1records = from rec1 in context1.Table1
                    select rec1;

var table2records = from rec2 in context2.Table2
                    select rec2;

var combined = from rec1 in table1records
               join rec2 in table2records on rec1.idColumn equals rec2.idColumn
               select new
               {
                 Rec1 = rec1,
                 Rec2 = rec2
               };
GalacticCowboy
A: 

You can create a View in DB1 that references table2 on DB2.

uvita