LINQ - Multiple Datasources

Posted on 2014-07-15
Last Modified: 2016-02-15

I need this SQL in LINQ.  Note the different databases, db1 & db2.

SELECT * FROM db1.tblA db1tA
INNER JOIN db2.tblA AS db2tA
ON db1tA.ColA = db2tA.ColA

The issues really is the JOIN from DB1 to DB2, LINQ does not like multiple DataContexts.

I am thinking I need to do this in 2 passes

First I will select what I need from DB2.tblA using LINQ and place it somewhere?
And then use what I created above and pass into some more LINQ and join to it.

Would this work?

I should say I am new to C# and this is my first bit of LINQ ever, so very much in at the deep end

Thanks in advance

Question by:nutnut
    LVL 62

    Expert Comment

    by:Fernando Soto
    Hi nutnut;

    You can send the raw SQL command to the SQL server and have the server execute the command. See the Linq to SQL code below.

    // Set up the Linq to SQL DataContext
    DataContext1 db = new DataContext1();
    // Create a string which has the T-SQL command you wish to execute on the SQL server
    string insertStatement = "NSERT INTO db1.tblB SELECT * FROM db1.tblA db1tA INNER JOIN db2.tblA AS db2tAON db1tA.ColA = db2tA.ColA";
    // Send the command to the server as a raw SQL command for execution

    Open in new window


    Author Comment

    Thanks I wish I could but company policy states we have to use LINQ, in case the DB is changed in the future.
    LVL 62

    Accepted Solution

    Well then what you stated in your question then should work. Please note that getting the values on the first pass and then passing it in to the SQL server, the inserts will happen one insert per record to be inserted. In other words if there are 100 records to be inserted into the database then there will be 100 commands sent to the server one record per command.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now