简体   繁体   中英

How to get data when there is handled errors

I have a procedure, which lists bunch of invoices and then calls another procedure for each one to process them. If there is a configuration problem, the processing procedure may fail. To solve this, the proccessing procedure is run in a transaction and is rolled back, if there is an error. After going through all the invoices, the listing procedure returns a list of invoice ids and flags to show which failed and which were ok.

My problem is that when I call this listing procedure in a C# program, and one of the processing procedures fails, the error message passes through and I cannot access the recordset containing list of invoice ids and error flags. Procedure works in Management Studio: The error is shown in the messages tab, but results tab also show the returned invoice list. But in the code DataAdapter.Fill() method fails and that's it. How can I retrieve the invoice list in that case? Or can I clear the error message somehow?

NOTE: It has to work in SQL 2000 and 2008.

I believe this is the most important point you need to keep in mind: Your procedure should not be returning an error when there is an error processing an invoice: since an invoice is a buisiness element in your program, this should be considered an "expected" or "expactable" error, which should be correctly handled. In other words, you should use return parameters, in this case, to return an error number and possibly an error description back to the caller, while still allowing you to obtain a resultset. Or even better, the presence of a "failed invoice" in the result set should be enough for your calling method to determine something went wrong.... To answer your question, yes, I do believe a DataAdapter flushes the result stream as soon as it detects an error (not sure though!).... I also believe you would have the same problem with a call using an ADO command & connection....

Just always keep this in mind: If you can predict that an error COULD occur in a procedure (wether SQL or in any programming language), you should not let the error be raised, but handle it there and use return parameters to tell the calling method that something went wrong.

So I would rewrite your SQL proc to : 1) Store your resultset in a table variable 2) catch the error 3) store the error info in return params 4) rollback the proc 5) Generate the resultset from the variable table

You will then be able to get an error message and # from your output params, PLUS get your result set. But again, the resultset containing errors should be enought for your calling method to detect that an "EXPECTED" error occured....

I know this is not perfect or complete as a solution, but as a general idea, it should work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM