views:

156

answers:

4

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.

A: 

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

leppie
A: 

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.

horseman
A: 

Dude, did you hit F5 and Debug?

Charles Gardner
A: 

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

Joe