Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecuteNonQuery doesn't throw exception on insert

I'm using OracleCommand.ExecuteNonQuery() to insert into a table. Everything works fine but occasionally records are not inserted. So is it possible that ExecuteNonQuery() doesn't insert the record and also doesn't throw an exception?

I'm not using stored procedures. I'm not using a transaction. I am logging any exception that is thrown by ExecuteNonQuery() but apparently no exception is thrown... I am also not checking the return value of ExecuteNonQuery(). But is it possible that ExecuteNonQuery returns another value than 1 if the insert is successful?

like image 308
Elz Avatar asked Mar 15 '26 19:03

Elz


2 Answers

It shouldn't. ExecuteNonQuery returns an integer indicating the number of rows affected. If no rows were inserted, 0 should be returned. It should thrown an exception when it fails to execute the query. E.x.: the connection is closed, the table doesn't exist, etc.

like image 73
Hasani Blackwell Avatar answered Mar 17 '26 08:03

Hasani Blackwell


Not unless you're swallowing the exception somewhere, like this:

try
{
   DoSomethingOnTheDbThatMightThrowAnException();
}
catch(Exception ex)
{
   // Do nothing, thus swallowing the exception
}

Also, check the return value of ExecuteNonQuery(). If it is 0, you might want to consider throwing an exception yourself.

like image 40
Neil Barnwell Avatar answered Mar 17 '26 08:03

Neil Barnwell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!