Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All

I have stored procedure which contains two procedures.
Say select Query1 and select query2.
I want to retrieve the second select query with Execute Reader.
Is that possible in sql server.

Can anyone help me



Regards
Froxy
Posted

Hi,

you can perform NextResult on your SqlCommand to advance your Reader.

C#
var result = sqlCommand.ExecuteReader();
if (result.NextResult())
{ 
// "result" variable contain next query resultset from DataSet
}


hope this will help you,

thanks
-Amit.
 
Share this answer
 
Comments
Amir Mahfoozi 28-Dec-11 5:32am    
+5 Well done.
AmitGajjar 28-Dec-11 6:23am    
Thank you :)
Dear Froxy,

You just have to do this:-

    SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");

SqlCommand salesCMD = new SqlCommand("SalesByCategory", nwindConn);
salesCMD.CommandType = CommandType.StoredProcedure;

SqlParameter myParm = salesCMD.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15);
myParm.Value = "Beverages";

nwindConn.Open();

SqlDataReader myReader = salesCMD.ExecuteReader().Table[1];

Console.WriteLine("{0}, {1}", myReader.GetName(0), myReader.GetName(1));

while (myReader.Read())
{
  Console.WriteLine("{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1));
}

myReader.Close();
nwindConn.Close();


I hope this will help you out

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900