Monday, March 7, 2011

When will SQLServer 2005 fail to do insert / update AND ALSO WILL NOT "throw" an exception?

After running a insert or update query against a SQLServer 2005 database, under what scenario (if at all) can this happen - SQLServer 2005 does not perform or fails to perform the insert / update, AND ALSO DOES NOT "THROW" AN EXCEPTION?

Consider that we are running the insert or update query via a SqlCommand object. Also consider that the table against which insert or update query is being used, has

  • identity fields
  • fields which are not null,
  • fields with appropriate data-types
  • fields which are reference keys
  • fields which have check constraints applied
  • etc.

Please assume that in the update query, the "WHERE" condition will find a recrod that needs to be updated.

Can we assume that after we do a SqlCommand.ExecuteNonQuery(), if a insert or update is not performed, then, an un-handled exception will ALLWAYS occur? Otherwise, we can assume that the insert or update query has been successful.

From stackoverflow
  • Look at the return value of ExecuteNonQuery() to see the number of records affected.

  • Move your logic to Database stored procedures and throw exceptions(RAISERROR) from there. Then you command(which would call corresponding stored procedure) would have necessary exceptions.

  • Dude, did you hit F5 and Debug?

  • You can assume that an exception (SqlException) will be thrown UNLESS the INSERT/UPDATE includes a WHERE clause that doesn't match any rows.

0 comments:

Post a Comment