开发者

Exceptions specific for SQL Server database operations

When catching an exception on a database reading/writing operation, what specific exceptions should I try to catch in a Try-Catch construction?

try
{
   MyDatabaseHandlingMethod();             
}
catch (exception ex)//what to use instead of just "exception"?
{
    //except开发者_开发百科ion handling
}

What are those exceptions to use instead of just "exception" in case of trying to perform a database related operation? (like IOException in case of file handling operations)


SQL Server exceptions will be of type SqlException - they contain an Errors collection that holds a list of SqlError objects that contain the detailed information about the exception.

For reference see MSDN:

  • System.Data.SqlClient.SqlException
  • System.Data.SqlClient.SqlError


You probably need to handle SqlException that has Number property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜