Dapper GridReader already fixed error

I use Dapper dot net to execute a stored procedure that returns 4 result sets. Here is how I do it:

public Results Search(Query query) { if (query == null) throw new ArgumentNullException("query"); Results results; var q = _sqlConnection.QueryMultiple("MySchema.MySproc", query, commandType: CommandType.StoredProcedure); { results = q.Read<Results>().First(); results.CheckAlertResults = q.Read<Results.CheckAlertResult>().ToArray(); // Cannot access a disposed object.Object name: 'GridReader'. results.PersonAlertResultRows = q.Read<Results.PersonAlertResultRow>().ToArray(); results.RoutingAlertResults = q.Read<Results.RoutingAlertResult>().ToArray(); } return results; } 

The first result set will contain only 1 row. This corresponds to a few primitive properties in my Results class.

The remaining 3 result sets will have many rows and will be filled with 3 complex array properties in the Results class.

For some reason I get

Unable to access the remote object. Object Name: "GridReader".

Check out my code to see where.

I checked that the procedure works correctly when calling LinqPad, which uses Linq2Sql.

What am I doing wrong?

+4
source share
1 answer

The reader will dispose of himself when he fails to find more resulting grids; basically, your C # code looks good, but the suggestion is that your proc only returns 1 grid. The following works just fine, for example:

 using (var reader = connection.QueryMultiple( "select 1; select 2 where 1 = 0; select 3 where 1 = 0; select 4;")) { var one = reader.Read<int>().ToArray(); var two = reader.Read<int>().ToArray(); var three = reader.Read<int>().ToArray(); var four = reader.Read<int>().ToArray(); try { // only returned four grids; expect a fifth read to fail reader.Read<int>(); throw new InvalidOperationException("this should not have worked!"); } catch (ObjectDisposedException) {/* expected; success */} one.Length.IsEqualTo(1); one[0].IsEqualTo(1); two.Length.IsEqualTo(0); three.Length.IsEqualTo(0); four.Length.IsEqualTo(1); four[0].IsEqualTo(4); } 

I can try to improve the error message, but I suspect that the error is in your SP.

+5
source

Source: https://habr.com/ru/post/1438774/


All Articles