Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7813883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:00:57+00:00 2026-06-02T05:00:57+00:00

If I want to run multiple SELECT queries on different tables, can I use

  • 0

If I want to run multiple SELECT queries on different tables, can I use the same SqlDataReader and SqlConnection for all of them?? Would the following be wise?? (I typed this up fast, so it lacks try/catch):

MySqlCommand myCommand = new MySqlCommand("SELECT * FROM table1", myConnection);

myConnection.Open();
SqlDataReader myDataReader = myCommand.ExecuteReader();

while(myReader.Read())
{
    //Perform work.
}

myCommand.commandText = "SELECT * FROM table2";

myReader = myCommand.ExecuteReader();

while(myReader.Read())
{
    //Perform more work
}

myReader.Close();
myConnection.Close();

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:00:59+00:00Added an answer on June 2, 2026 at 5:00 am

    You can use the same connection for each of them, as long as you do not try to execute multiple queries concurrently on the same connection from different threads.

    As for the data reader, you are not actually re-using the reader, each call to ExecuteReader returns a new instance of a new reader, all you are re-using is the variable that maintains the reference to the reader. Here in lies a problem, you are only explicitly closing the last reader and leaving the first to be GC’d at some later time.

    You can reuse the Command as well, but remember if you supply parameters etc. you will need to clear them for the next query unless they apply to the next query as well.

    You should use try/finally blocks to ensure that you clean up the resources, or here is a quick change to your code to use using statements to ensure resource clean-up even if there is an exception that prevents the rest of the code from executing.

    using (var myConnection = GetTheConnection())
    {
      myConnection.Open();
    
      var myCommand = new MySqlCommand("SELECT * FROM table1", myConnection))
      using (var myDataReader = myCommand.ExecuteReader())
      {
        while(myReader.Read())
        {
          //Perform work.
        }
      } // Reader will be Disposed/Closed here
    
      myCommand.commandText = "SELECT * FROM table2";
      using (var myReader = myCommand.ExecuteReader())
      {
        while(myReader.Read())
        {
          //Perform more work
        }
      } // Reader will be Disposed/Closed here
    } // Connection will be Disposed/Closed here
    

    Note: GetTheConnection is just a place holder function for what ever mechanism you are using to get your connection instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run select query on multiple table and combine result. for example
I want select multiple (all) values from table Account. string query = SELECT *
I want to run multiple instances of apache on one single machine? How to
I want to write a method that run multiple threads and I want before
I can use the following code for tiny little queries: DECLARE @sql VARCHAR(8000) SET
We can use prepare method to get the query prepared for multiple time use
I want to run a script in my macro multiple times by changing variable
Looking to run a query using multiple left joins. The following query works. SELECT
How can know, if select( Choice Or Browse... ) file in input:file run some
I want to delete the records which i select, and run.html will refresh, how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.