简体   繁体   English

处理错误时如何获取数据

[英]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. 查看完所有发票后,列表过程将返回发票ID和标志的列表,以显示哪些失败和哪些可以。

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. 我的问题是,当我在C#程序中调用此列表过程,而其中一个处理过程失败时,错误消息就会通过,并且我无法访问包含发票ID和错误标志列表的记录集。 Procedure works in Management Studio: The error is shown in the messages tab, but results tab also show the returned invoice list. 该过程在Management Studio中起作用:错误显示在消息选项卡中,但是结果选项卡还显示返回的发票列表。 But in the code DataAdapter.Fill() method fails and that's it. 但是在代码中,DataAdapter.Fill()方法失败了,仅此而已。 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. 注意:它必须在SQL 2000和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.... 甚至更好的是,结果集中存在“失败的发票”应该足以使您的调用方法确定出了问题。...要回答您的问题,是的,我确实相信DataAdapter会尽快刷新结果流因为它检测到错误(虽然不确定!)。...我也相信您在使用ADO命令和连接进行调用时也会遇到同样的问题。

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. 始终牢记这一点:如果可以预测某个过程(无论是SQL还是任何编程语言)中都可能发生错误,则不应让该错误发生,而应在此处进行处理并使用返回参数来告知调用出问题的方法。

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 因此,我将您的SQL proc重写为:1)将结果集存储在表变量中2)捕获错误3)将错误信息存储在返回参数中4)回滚proc 5)从变量表生成结果集

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.... 但是同样,包含错误的结果集应该足以让您的调用方法检测到发生了“ EXPECTED”错误。

I know this is not perfect or complete as a solution, but as a general idea, it should work! 我知道这不是一个完美的解决方案,但作为一个总体思路,它应该可以工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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