简体   繁体   English

在C#中从SQL捕获异常的最佳实践

[英]Best practices for catching exceptions from SQL in C#

What are some good ways to catch business logic exceptions or return values from SQL in C#? 在C#中捕获业务逻辑异常或从SQL返回值有什么好方法? For instance, upon creating a new user, if the user already exists, the system and the user must be notified. 例如,在创建新用户时,如果用户已经存在,则必须通知系统和用户。 I have used the raise_error() method with a particular state int value, I have used a stored procedure returning int values, and I have also selected a msg column with a particular structure. 我已经将raise_error()方法与特定的状态int值一起使用,我使用了一个返回int值的存储过程,并且我还选择了一个具有特定结构的msg列。 Are there any best practices for this? 对此有什么最佳做法吗?

i always do two things: 我总是做两件事:

  1. use raiseerror, which manifests on the code side as an exception, and 使用raiseerror,它在代码端显示为异常,并且
  2. return an error code 返回错误代码

this lets me catch errors both in C# code and also when one stored procedure calls another 这让我可以在C#代码中捕获错误,也可以在一个存储过程调用另一个存储过

[sql server now supports a try-catch block, but i haven't used it yet] [sql server现在支持try-catch块,但我还没用过它]

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

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