LINQ to SQL: Errors from a stored procedure are ignored after returning a result set

I am using LINQ to SQL to call a stored procedure. This stored procedure currently returns a result set, and then has several statements raiserrorthat run after receiving the results (I write tests for stored procedures if you are wondering why I am doing this).

When LINQ to SQL calls proc and returns the result, it seems to ignore all the errors that I throw because it got its result set. Is there a way to make it always throw a SqlException when I do raiserrorfrom SQL?

+3
source share
2 answers

Interesting; this is a problem that I saw earlier when using IDataReader, so I now religiously consume all tables (even if I only expect them) - for example, if I only expect one table, something like:

    while (reader.Read())
    { // read data from first table

    }
    // read to end of stream
    while (reader.NextResult()) { }

The problem is that the error goes to TDS the moment you raise it; therefore, if you pick it up later SELECT, and then in the table in TDS, and if the reader does not read it until the end of the stream, they may not see it.

- : . , SELECT temp-table (#table) table-variable (@table). - , ( LINQ-to-SQL ), , ExecuteReader - .

( ), DataContext.Translate<T> ORM; :

// cmd is our DbCommand, and ctx is our DataContext
using(var reader = cmd.ExecuteReader()) {
   var result = ctx.Translate<MyResultType>(reader).ToList();
   while(reader.NextResult()) {}
   return result;
}
+6

, 10, RAISERROR :

http://support.microsoft.com/default.aspx/kb/321903

RAISERROR('Stored Procedure Execution Failed',15,1) 
+2

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


All Articles