我编写了存储过程,返回两个结果集。 我用dapper API从C#代码调用了stord过程
像这样var Count= result.Read<int>().ToList().FirstOrDefault();
var Employees= result.Read<EmployeeData>().ToList();
问题是
如果在存储过程中我直接返回正常查询,那么dapper api工作正常
SELECT COUNT(ID) FROM Employee
SELECT EmpId,EmpName From Employee
但是当我更改stord过程并添加一些动态查询时,它会在读取第二个结果时出错
更改了SQL
SET @Query ='SELECT COUNT(ID) FROM Employee;
SELECT EmpId,EmpName From Employee'
exec (@Query);
两个approches的结果在sql中相同
但在C#中使用dapper API对于第一种情况它工作正常但是对于第二种approch(使用动态查询)它在第二次读取statemnet时获得休息
var Count= result.Read<int>().ToList().FirstOrDefault();
var Employees= result.Read<EmployeeData>().ToList();<<= for second approach breaking here
错误消息:“读者已被处理;这可能在所有数据消耗后发生。对象名称:'Dapper.SqlMapper + GridReader'。”
答案 0 :(得分:4)
试试这个&#34;使用QueryMultiple
&#34;
void Main()
{
var sql = @"
--create test data
CREATE TABLE #Employee
([ID] int, [EmpId] int, [EmpName] varchar(3))
;
INSERT INTO #Employee
([ID], [EmpId], [EmpName])
VALUES
(1, 1, 'xxx'),
(2, 2, 'bbb')
;
--sql
declare @Query nvarchar(max);
SET @Query ='SELECT COUNT(ID) FROM #Employee;
SELECT EmpId,EmpName From #Employee'
exec (@Query);
";
var result = this.Connection.QueryMultiple(sql);
var Count = result.Read<int>().ToList().FirstOrDefault();
var Employees = result.Read<Employee>().ToList();
}
// Define other methods and classes here
public class Employee
{
public int? ID { get; set; }
public int? EmpId { get; set; }
public string EmpName { get; set; }
}