如果使用SqlDataAdapter.Fill()
Method 填充 DataSet,则从存储过程返回的每个记录集都将作为数据集中的 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;
}
}
}