Catching SQL unique key exceptions in .NET

后端 未结 4 2093
自闭症患者
自闭症患者 2020-12-28 21:40

I was wondering if anyone had a more elegant way to check for unique key exceptions from SQL in .NET other than parsing the error message? Right now I am calling the sproc

4条回答
  •  情深已故
    2020-12-28 22:19

    Obvious answer: Attempt to fetch that value from the database before inserting (or updating) it and notify the user before you do the insert.

    Alternative answer: Check for the uniqueness inside the SP and return that as an error message from the SP, then you don't have to parse any errors. They're all bad.

提交回复
热议问题