locked
How to execute two queries at a time

    Question

  • Suppose i have a query

     

    MySqlCommand cmd = con.CreateCommand();
    cmd.CommandText = "select Qty - " + qty + " from " + comboBox1.Text + " where Type = '" + comboBox3.Text + "' and Manufacturer = '" + comboBox4.Text + "' and Model = '" + comboBox5.Text + "'; Select price from model where Tpe = '" + comboBox3.Text + "' and Cpy = '" + comboBox4.Text + "' and Mdl = '" + comboBox5.Text + "';";
    MySqlDataReader rd = cmd.ExecuteReader();
    int qa = Convert.ToInt32(rd[0]);
    rd.Close();
    cmd.Dispose();
    

     

    In this i want the data from the second select command to be stored in a variable, just like the first select statement is stored in qa

     



    Thursday, August 25, 2011 9:52 AM

Answers

  • Do you want some thing like this


       
    using (var reader = cmd.ExecuteReader())
       
    {
           
    do
           
    {
               
    while (reader.Read())
               
    {
                   
    Console.WriteLine(reader.GetInt32(0));
               
    }
               
    Console.WriteLine("--next command--");
           
    } while (reader.NextResult());

       
    }


    • Marked as answer by Qasim Shah Friday, August 26, 2011 12:21 PM
    Thursday, August 25, 2011 10:26 AM

All replies

  • Hello Qasim

    See this code

    SqlDataAdapter   dataAdapter = new SqlDataAdapter();

            string query = "select Qty - " + qty + " from " + comboBox1.Text + " where Type = '" + comboBox3.Text + "' and Manufacturer = '" + comboBox4.Text + "' and Model = '" + comboBox5.Text + "'; Select price from model where Tpe = '" + comboBox3.Text + "' and Cpy = '" + comboBox4.Text + "' and Mdl = '" + comboBox5.Text + "';";

            adapter = new SqlDataAdapter(query, connection);

            DataSet dataSet = new DataSet();

            dataAdapter.Fill(dataSet);

            // You will get price from second table of dataset

            string price = ds.Tables[1].Rows[0]["price"].ToString();

     


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Thursday, August 25, 2011 10:23 AM
  • Do you want some thing like this


       
    using (var reader = cmd.ExecuteReader())
       
    {
           
    do
           
    {
               
    while (reader.Read())
               
    {
                   
    Console.WriteLine(reader.GetInt32(0));
               
    }
               
    Console.WriteLine("--next command--");
           
    } while (reader.NextResult());

       
    }


    • Marked as answer by Qasim Shah Friday, August 26, 2011 12:21 PM
    Thursday, August 25, 2011 10:26 AM
  • Hi,

    As Zain told see http://msdn.microsoft.com/en-us/library/haa3afyz.aspx and the Retrieving Multiple Result sets using NextResult. Another option would be to combine both select to return a single resultset with two columns (I suppose that both requests should always return a single row ?) by using either a join or a subquery.

    Basically :

    select col1,(select col1 from otherTable Where etc...) from MyTable where etc...

    or select t1.col1,t2col1 from t1 join t2 on t2.key=t1.key where t1.key= etc...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Thursday, August 25, 2011 10:50 AM