本文介绍了如何从SqlDataReader中读取多个结果集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个SP,我试图从中返回2个结果集,而在我的.cs文件中,我正在尝试如下操作:
I have a SP from that I am trying to return 2 result set from, and in my .cs file i am trying something like this:
dr = cmd.ExecuteReader();
while (dr.Read())
{
RegistrationDetails regDetails = new RegistrationDetails()
{
FName = dr["FName"].ToString(),
LName = dr["LName"].ToString(),
MName = dr["MName"].ToString(),
EntityName = dr["EntityName"].ToString(),// in 2nd result set
Percentage = dr["Percentage"].ToString()// in 2nd result set
};
}
但是我得到一个:
推荐答案
这里有一个有关如何使用数据读取器处理多个结果集的示例
Here you have a sample about how to handle multiple result sets with a data reader
static void RetrieveMultipleResults(SqlConnection connection)
{
using (connection)
{
SqlCommand command = new SqlCommand(
"SELECT CategoryID, CategoryName FROM dbo.Categories;" +
"SELECT EmployeeID, LastName FROM dbo.Employees",
connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
do
{
Console.WriteLine("\t{0}\t{1}", reader.GetName(0),
reader.GetName(1));
while (reader.Read())
{
Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0),
reader.GetString(1));
}
}
while (reader.NextResult());
}
}
从多个数据集中检索数据的关键是使用 reader.NextResult
The key for retrieving data from multiple data sets is using reader.NextResult
这篇关于如何从SqlDataReader中读取多个结果集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!