メソッドを使用して DataSet を埋める場合SqlDataAdapter.Fill()
、ストアド プロシージャから返された各レコードセットは、データセット内の DataTable として返されます。
DataSet dataset = new DataSet();
using (var adapter = new SqlDataAdapter("yourStoredProcedure", yourConnectionString))
{
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.Fill(dataset);
}
for (int i = 0; i < dataset.Tables.Count; i++)
{
// Do something for each recordset
}
SqlDataReader を使用する場合、SqlDataReader.NextResult()
メソッドを使用して次のレコードセットに進むことができます。
using (var connection = new SqlConnection(yourConnectionString))
using (var command = new SqlCommand("yourStoredProcedure"))
{
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
// do something with first result set;
}
if (reader.NextResult())
{
while (reader.Read())
{
// do something with second result set;
}
}
else
{
return;
}
if (reader.NextResult())
{
while (reader.Read())
{
// do something with third result set;
}
}
else
{
return;
}
}
}