How to catch SQLServer timeout exceptions

后端 未结 6 2040
轮回少年
轮回少年 2020-11-27 11:32

I need to specifically catch SQL server timeout exceptions so that they can be handled differently. I know I could catch the SqlException and then check if the message stri

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 11:52

    Updated for c# 6:

        try
        {
            // some code
        }
        catch (SqlException ex) when (ex.Number == -2)  // -2 is a sql timeout
        {
            // handle timeout
        }
    

    Very simple and nice to look at!!

提交回复
热议问题