Tuesday, December 24, 2013

Processing multiple result sets with SqlDataReader

According to
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.nextresult(v=vs.110).aspx
use NextResult() to advance the reader to the next result set


pseudo-code:
//I have one stored proc called sp_GetAllResults that calls three other stored procs
SqlCommand cmd = new SqlCommand("sp_GetAllResults");
cmd.CommandType = CommandType.StoredProcedure;
//i am actually using one stored proc, which calls several stored procs, so it's more like

SqlDataReader reader = cmd.ExecuteReader();
//Process result set 1
while (reader.Read())
{
//do stuff
}

//Move to next result set
reader.NextResult();
//Process result set 2
while (reader.Read())
{
//do stuff
}

//Move to the third result set
reader.NextResult();
//Process result set 3
while (reader.Read())
{
//do stuff
}

No comments:

Post a Comment

There was an error in this gadget