MySqlConnection con = GetConnection();
string sql = "SELECT COUNT(ID) FROM booking WHERE DATE(Booking_Date) = CURDATE()";
string sql2 = "SELECT SUM(Fare) FROM booking WHERE DATE(Booking_Date) = CURDATE()";
MySqlCommand cmd = new MySqlCommand(sql, con);
cmd.CommandType = System.Data.CommandType.Text;
MySqlDataReader BookingToday = cmd.ExecuteReader();
while (BookingToday.Read())
{
label6.Text = BookingToday.GetValue(0).ToString();
}
This is my C# code and I want to run both the given queries to get result at once. But I don’t know how to run multiple data readers on single connection or run 2 connections at once. Anyone please help me in this regard
3
Answers
You can use one query:
In this specific case: you don’t need to – you can use the code shown in slaakso’s answer to perform both aggregates in one query.
In the more general case:
i.e. sequentially and not overlapping (unless your provider supports the equivalent of "MARS").
You can also often issue multiple queries (
select
) in a single command; you useNextResult()
to move between them:You can’t open multiple data readers simultaneously from a single connection.
If you want a single result, you can use ExecuteScalar.
For multi-row results, you can store them in a DataTable.