Is it possible to combine multiple requests in .net so that they are returned in 1 batch?

I want to execute multiple select sql queries over 1 database connection (SQL Server). The queries will contain different table data, so this should not be done using sql.

Basically I want to do it like this

select * from Table1
Go
Select * from Table2
Go
Select * from Table3

      

then send this query to the database and then I want the result to be returned in one hit, so I can read the data using 3 datareaders.

Is this possible with C # and sqlserver?

+2


source to share


3 answers


GO is not a SQL statement. Therefore, you must send 3 requests to the server as one batch:



SqlCommand cmd = new SqlCommand(
@"select * from Table1;
select * from Table2;
select * from Table3;", connection);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
   while (rdr.Read())
   {
     //read results from Table1
   };
   rdr.NextResult();
   while(rdr.Read())
   {
    //read results from Table2
   }
   rdr.NextResult();
   while(rdr.Read())
   {
     //read results from Table3
   }
 }

      

+2


source


You can use multiple active result sets: http://www.codeguru.com/csharp/csharp/cs_network/database/article.php/c8715



+2


source


+2


source







All Articles