[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 600
  • Last Modified:

Retrieve Many Tables Without Associations

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.
  • 4
1 Solution
Fernando SotoCommented:
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

Fernando SotoCommented:
This line is not needed:

db.Log = Console.Out;
Fernando SotoCommented:
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"?
demiketAuthor Commented:
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.
Fernando SotoCommented:
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.

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now