We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Retrieve Many Tables Without Associations

demiket
demiket asked
on
Medium Priority
610 Views
Last Modified: 2013-12-17
I know that with LoadOptions I can retrieve data from tables with associations using one only connection.
Example:
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:
datacontext1.GetTable<Customer>();
datacontext1.GetTable<Supplier>();
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.
Comment
Watch Question

Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
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");
            cnn.Open();
            DataClasses1DataContext db = new DataClasses1DataContext(cnn);
            db.Log = Console.Out;
            db.GetTable<Customers>();
            db.GetTable<Suppliers>();

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

            // You must remember to close the connection
            cnn.Close();

Fernando

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
This line is not needed:

db.Log = Console.Out;
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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

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.
Sorry!
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.