简体   繁体   English

将 SQL `raiserror` 与 C# 中的 `select` 组合

[英]Combining SQL `raiserror` with `select` in C#

I want to know if there is a way of using both SQL raiserror and retrieving the selected result in C#.我想知道是否有一种方法可以同时使用 SQL raiserror并在 C# 中检索所选结果。 ExecuteReader() in C# will throw an exception when raiserror occurs, but I still want to use the reader to capture any data returned. C# 中的ExecuteReader()raiserror发生时会抛出异常,但我仍然想使用 reader 来捕获返回的任何数据。

Below is a simplified example.下面是一个简化的例子。 If this is not possible I will use raiserror for general cases and select for specific cases.如果这不可能,我将在一般情况下使用raiserror ,在特定情况下使用select

if (some-error)
begin
    select  @Message = 'ERROR: script made a booboo',
            @State = 'State Info'
    raiserror (@Messsage, 16, 1)
    goto exit_sp
end

exit_sp:
    select  @Message 'Message', @State 'State'

If you lower the severity to 10 or below (from memory) it will not raise an exception, but will be available via the InfoMessage event on the connection .如果您将严重性降低到 10 或以下(从内存中),它不会引发异常,但可以通过 connection 上的InfoMessage 事件获得 Note, however, that because of how TDS works, you should ensure that you Read() etc to the end of all results;但是请注意,由于 TDS 的工作方式,您应该确保Read()等到所有结果的末尾 for example, if you have multiple select s and then a raiserror , and you only read the first select before dropping the data-reader, there is a chance you won't see the message (the TDS will be killed).例如,如果您有多个select然后raiserror ,并且您在删除数据阅读器之前只阅读了第一个select ,那么您有可能看不到该消息(TDS 将被杀死)。

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

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