我正在使用QueryMultiple
,它会返回GridReader。
由于我不知道我要阅读多少数据,因此我在IsConsumed
的停止条件下循环读取:
using (var reader = conn.QueryMultiple(mySql)) {
while(!reader.IsConsumed) {
reader.Read<...>
}
}
但是,我在上次阅读时总是得到ObjectDisposedException
。 IsConsumed
的值仍为false
。
我试图将DynamicParameters
传递给查询,目的是获取回调(这似乎通过IParameterCallbacks
有用),但我无法将其一起修补。
我真的不想在代码中遇到这样的预期异常。谢谢你的帮助。
我正在使用SQL Server,我的提供程序是.NET 4.5中的System.Data.SqlClient
,Dapper版本1.40.0.0
例如失败的测试:
[TestMethod]
public void QueryMultipleWithCursor()
{
const string sql = @"
DECLARE @CurrentDate DATE
DECLARE DatesCursor CURSOR LOCAL FOR
SELECT DISTINCT DataDate FROM Data_Table ORDER BY DataDate
OPEN DatesCursor
FETCH NEXT FROM DatesCursor INTO @CurrentDate
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT DISTINCT
DataDate AS Date1,
DataDate AS Date2
FROM Data_Table
WHERE DataDate=@CurrentDate
FETCH NEXT FROM DatesCursor INTO @CurrentDate
END
CLOSE DatesCursor
DEALLOCATE DatesCursor";
using (var conn = _database.GetConnection())
{
var reader = conn.QueryMultiple(sql);
while (!reader.IsConsumed)
{
reader.Read<DateTime, DateTime, DateTime>(
(date1, date2) => date1,
splitOn: "Date2").ToList();
}
}
}
我正在使用以下堆栈获得NullReferenceException
:
at Dapper.SqlMapper.GridReader.NextResult() in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 4440
at Dapper.SqlMapper.GridReader.<MultiReadInternal>d__9`8.System.IDisposable.Dispose()
at Dapper.SqlMapper.GridReader.<MultiReadInternal>d__9`8.MoveNext() in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 4309
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.GridReader.Read[TFirst,TSecond,TReturn](Func`3 func, String splitOn, Boolean buffered) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 4330
at Project.MyTests.QueryMultipleWithCursor() in C:\Project\MyTests.cs:line 171
Result Message:
Test method Project.MyTests.QueryMultipleWithCursor threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
答案 0 :(得分:3)
我已经推送了以下内容,它传递给SQL Server / SqlConnection
;所以它可以工作:
[Fact]
public void SO35554284_QueryMultipleUntilConsumed()
{
using (var reader = connection.QueryMultiple(
"select 1 as Id; select 2 as Id; select 3 as Id;"))
{
List<HazNameId> items = new List<HazNameId>();
while (!reader.IsConsumed)
{
items.AddRange(reader.Read<HazNameId>());
}
items.Count.IsEqualTo(3);
items[0].Id.IsEqualTo(1);
items[1].Id.IsEqualTo(2);
items[2].Id.IsEqualTo(3);
}
}
我想知道这里的问题是否是特定ADO.NET提供程序的问题。您可能想要完全指定:
答案 1 :(得分:1)
我遇到与Dapper相同的问题,我使用版本1.42.0和SQL Server 2012作为后端。在调试时,我发现仅当我们尝试使用最后一个结果集上的Dapper的splitOn选项创建多个对象时才会发生此问题。
我在GitHub上提交了一个新问题 https://github.com/StackExchange/dapper-dot-net/issues/469
答案 2 :(得分:0)
这似乎是Dapper实施的一个问题,因为我同时使用Dapper
和SqlDataReader
,这更可靠:
public static SqlMapper.GridReader QueryMultipleStoredProcedure(this IDbConnection dbConnection, string spName, object parameters, out SqlDataReader sqlDataReader)
{
var gridReader = dbConnection.QueryMultiple(spName, new DynamicParameters(parameters), commandType: CommandType.StoredProcedure);
sqlDataReader = typeof (SqlMapper.GridReader).GetInstanceField<SqlDataReader>(gridReader, "reader");
return gridReader;
}
private static T GetInstanceField<T>(this Type type, object instance, string fieldName)
{
var bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var field = type.GetField(fieldName, bindFlags);
return (T) field?.GetValue(instance);
}
然后我可以使用sqlDataReader.HasRows