SqlException具有您可以检查的Number属性。对于重复错误,数字为2601。
catch (SqlException e){ switch (e.Number) { case 2601: // Do something. break; default: throw; } }要获取服务器中所有SQL错误的列表,请尝试以下操作:
SELECt * FROM sysmessages
更新资料
现在可以在C#6.0中简化
catch (SqlException e) when (e.Number == 2601){ // Do something.}


