52

I am often comparing data in tables in different databases. These databases do not have the same schema. In TSQL, I can reference them with the DB>user>table structure (DB1.dbo.Stores, DB2.dbo.OtherPlaces) to pull the data for comparison. I like the idea of LINQPad quite a bit, but I just can't seem to easily pull data from two different data contexts within the same set of statements.

I've seen people suggest simply changing the connection string to pull the data from the other source into the current schema but, as I mentioned, this will not do. Did I just skip a page in the FAQ? This seems a fairly routine procedure to be unavailable to me.

In the "easy" world, I'd love to be able to simply reference the typed datacontext that LINQPad creates. Then I could simply:

DB1DataContext db1 = new DB1DataContext();
DB2DataContext db2 = new DB2DataContext();

And work from there.

6 Answers 6

72

Update: it's now possible to do cross-database SQL Server queries in LINQPad (from LINQPad v4.31, with a LINQPad Premium license). To use this feature, hold down the Control key while dragging databases from the Schema Explorer to the query window.

It's also possible to query linked servers (that you've linked by calling sp_add_linkedserver). To do this:

  1. Add a new LINQ to SQL connection.
  2. Choose Specify New or Existing Database and choose the primary database you want to query.
  3. Click the Include Additional Databases checkbox and pick the linked server(s) from the list.
8
  • 2
    "Error: Invalid object name 'sys.servers'." Apr 27, 2011 at 15:51
  • 20
    Users, keep in mind that it is cross-database, not cross-server - "Multi-database queries are supported only for SQL Server databases on the same server (or linked servers)." Apr 27, 2011 at 15:55
  • 7
    Would be nice to have support for multiple servers that are not linked just the way Scott describes with multiple data contexts. Apr 27, 2011 at 15:57
  • 3
    n.b. although this does work fine for Linq-to-SQL it won't work for DbContext. I realise the question is specifically for Linq-to-SQL, but just in case (Like me) anyone else doesn't read the question properly :-) Dec 5, 2012 at 16:19
  • 1
    Would be nice to have that feature in the free version Mar 3, 2015 at 22:30
8

Keep in mind that you can always create another context on your own.

public FooEntities GetFooContext()
{
   var entityBuilder = new EntityConnectionStringBuilder        
               {        
                    Provider = "Devart.Data.Oracle",        
                    ProviderConnectionString = "User Id=foo;Password=foo;Data Source=Foo.World;Connect Mode=Default;Direct=false",
                    Metadata = @"D:\FooModel.csdl|D:\FooModel.ssdl|D:\FooModel.msl"     
                };

    return new FooEntities(entityBuilder.ToString());
}
2
  • 4
    What is FooEntities? can I write this code inside LINQPad?if so, how? Jan 24, 2018 at 6:35
  • This answer silently assumes that you have created an Assembly containing a class FooEntities. How would it work if you just have a connection string? And which NUGET package do you need to add to have EntityConnectionStringBuilder ?
    – Matt
    Aug 12, 2021 at 7:49
5

You can instantiate as many contexts as you like to disparate SQL instances and execute pseudo cross database joins, copy data, etc. Note, joins across contexts are performed locally so you must call ToList(), ToArray(), etc to execute the queries using their respective data sources individually before joining. In other words if you "inner" join 10 rows from DB1.TABLE1 with 20 rows from DB2.TABLE2, both sets (all 30 rows) must be pulled into memory on your local machine before Linq performs the join and returns the related/intersecting set (20 rows max per example).

//EF6 context not selected in Linqpad Connection dropdown
var remoteContext = new YourContext();
remoteContext.Database.Connection.ConnectionString = "Server=[SERVER];Database="
+ "[DATABASE];Trusted_Connection=false;User ID=[SQLAUTHUSERID];Password=" 
+ "[SQLAUTHPASSWORD];Encrypt=True;";
remoteContext.Database.Connection.Open();
var DB1 = new Repository(remoteContext);

//EF6 connection to remote database
var remote = DB1.GetAll<Table1>()
    .Where(x=>x.Id==123)
    //note...depending on the default Linqpad connection you may get 
    //"EntityWrapperWithoutRelationships" results for 
    //results that include a complex type.  you can use a Select() projection 
    //to specify only simple type columns
    .Select(x=>new { x.Col1, x.Col1, etc... })
    .Take(1)
    .ToList().Dump();  // you must execute query by calling ToList(), ToArray(),
              // etc before joining


//Linq-to-SQL default connection selected in Linqpad Connection dropdown
Table2.Where(x=>x.Id = 123)
    .ToList() // you must execute query by calling ToList(), ToArray(),
              // etc before joining
    .Join(remote, a=> a.d, b=> (short?)b.Id, (a,b)=>new{b.Col1, b.Col2, a.Col1})
    .Dump();
        
remoteContext.Database.Connection.Close();
remoteContext = null;
2
  • 1
    Did you mean to close and null localContext or remoteContext at the bottom? Also, where is YourContext defined?
    – NetMage
    Apr 17, 2017 at 21:28
  • I know this is a late reply to the comment above, but YourContext would be defined somewhere needed. I believe localContext should have been remoteContext. from how I use it, localContext would be the assigned connection of the Linqpad file, and remoteContext is the "2nd" context. Thanks @Adam
    – AceMark
    Sep 9, 2020 at 21:29
3

I do not think you are able to do this. See this LinqPad request.

However, you could build multiple dbml files in a separate dll and reference them in LinqPad.

3
  • So far, this has been my conclusion as well. I was hoping I was wrong! Thanks much.
    – Scott
    Sep 30, 2009 at 16:00
  • the link is dead with a 404 Page not found error.
    – Mong Zhu
    Jan 22, 2018 at 7:36
  • 1
    @MongZhu I fixed the link, but the other answer is correct now.
    – NetMage
    Apr 18, 2018 at 21:51
3

Drag-and-drop approach: hold down the Ctrl key while dragging additional databases from the Schema Explorer to the query editor.

Use case:

//Access Northwind

var ID = new Guid("107cc232-0319-4cbe-b137-184c82ac6e12");

LotsOfData.Where(d => d.Id == ID).Dump();

//Access Northwind_v2

this.NORTHWIND_V2.LotsOfData.Where(d => d.Id == ID).Dump();
0
1

Multiple databases are as far as I know only available in the "paid" version of LinqPad (what I wrote applies to LinqPad 6 Premium).

For more details, see this answer in StackOverflow (section "Multiple database support").

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.