Check if Database raises a particular exception
using Nhibernate;
开发者_C百科I'm trying to insert several values a on table which has an unique index on some columns.
I'd like to know if a particular insert raises an exception for having violated the unique constraint.
So, which particular exception type should i catch? I only want to catch this particular one and let all others go up.
Thanks a lot!
I'm afraid that there is no special exception for that. You will have to catch ADO NET exceptions and look on the inner exception text.
IMHO your approach is not the more appropriate. You should query the DB in order to check BEFORE the insert if the data will violate the unique constraint. If it does, then you don't insert the record.
You need to implement ISQLExceptionConverter
.
Check Custom exception using NHibernate ISqlExceptionConverter and http://fabiomaulo.blogspot.com/2009/06/improving-ado-exception-management-in.html for examples.
精彩评论