Retrieve Many Tables Without Associations

Posted on 2009-02-18
Last Modified: 2013-12-17
I know that with LoadOptions I can retrieve data from tables with associations using one only connection.
LoadOptions1.LoadWith<Customer>(c => c.Orders);
LoadOptions1.LoadWith<Order>(o => o.Order_Details);

But if i want to retrieve data from two tables with NO ASSOCIATION using one only connection, how can I do?

For example, I want in a form a list of Customers and a list of Suppliers, but I want to load these values using ONE only connection, because my database is on a remote server, so I want to optimize my connections.
If I do:
looking at Sql Profile I see a first SELECT, a Connection Reset, second SELECT, a Connection Reset.
I want First and Second SELECT and a Reset Connection.
Question by:demiket
    LVL 62

    Accepted Solution

    Hi demiket;

    If you supply an open connection to the DataContext it will not close it between queries until you specifically close it.

                // If you provide an open connection, the DataContext will not close it.
                SqlConnection cnn = new SqlConnection(@"YOUR CONNECTION STRING HERE");
                DataClasses1DataContext db = new DataClasses1DataContext(cnn);
                db.Log = Console.Out;

                dataGridView1.DataSource = db.Customers;
                dataGridView2.DataSource = db.Suppliers;

                // You must remember to close the connection

    LVL 62

    Expert Comment

    by:Fernando Soto
    This line is not needed:

    db.Log = Console.Out;
    LVL 62

    Expert Comment

    by:Fernando Soto
    Can I ask a question of you. Did not what I posted answer you question? Did it not help you to move onto other things?

    Why the grade of "B"?

    Author Comment

    I don't know!

    These were the first times I used the valutations in Expert-Exchange: I have selected only the questions!
    Is it possible to change in A?

    I apologize for my mistake.
    LVL 62

    Expert Comment

    by:Fernando Soto
    It is not a problem, I just was wondering if I had not totally answered your question.

     From the EE Help page under "Asking Questions"

    What's the right grade to give?

    Grading at Experts Exchange is not like school. It's more like the "10-point Must" system in professional boxing; in other words, an answer is worth an A, unless it doesn't resolve your issue. If it requires you to do a little more research, or figure out one more piece of code, then it's worth a B. If you think it's not worth a B, the custom is to offer the Experts an opportunity to earn a better grade.

    Giving a higher grade has no impact on your Available Points.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    760 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

    7 Experts available now in Live!

    Get 1:1 Help Now