O
Olaf Rabbachin
Hi *,
using an OleDbCommand I issue a DELETE statement against an Access-MDB.
This roughly looks like this:
Try
Dim cmd As New OleDbCommand("qry_DeleteSomething", DB_Connection)
cmd.CommandText = "DELETE FROM tblXY WHERE ID = 123"
If DB_Connection.State <> ConnectionState.Open Then DB_Connection.Open()
Dim intRecsAffected As Integer = cmd.ExecuteNonQuery
DB_Connection.Close()
Catch exOLEDB As OleDbException
...
Catch ex As Exception
...
End Try
I would like to execute this code even if the DELETE might fail, i.e.
because there is a record in a different table that is linked to the one
that is to be deleted (i.e. attempting to delete a customer record while
there is invoices that are associated with that customer).
Instead, I was assuming that, just like what happens when you try to delete
a record from within Access, an exception is being raised. This doesn't
happen though - no exception to catch. Even worse, <intRecsAffected> in the
above sample will equal <1>, as if the underlying DELETE statement had been
executed successfully!?
If I do the exact same thing with a SQL Server database, I can catch
<SqlException.Number=547> if the DELETE fails.
Thus, how can I find out as to whether a DELETE was actually successful or
not? There just has to be something other than doing a SELECT after the
DELETE, just to confirm that the record isn't there anymore!?
Also, the same applies to INSERTs and UPDATEs where i.e. a unique index
prevents adding or changing a record ...
Cheers & TIA,
Olaf
using an OleDbCommand I issue a DELETE statement against an Access-MDB.
This roughly looks like this:
Try
Dim cmd As New OleDbCommand("qry_DeleteSomething", DB_Connection)
cmd.CommandText = "DELETE FROM tblXY WHERE ID = 123"
If DB_Connection.State <> ConnectionState.Open Then DB_Connection.Open()
Dim intRecsAffected As Integer = cmd.ExecuteNonQuery
DB_Connection.Close()
Catch exOLEDB As OleDbException
...
Catch ex As Exception
...
End Try
I would like to execute this code even if the DELETE might fail, i.e.
because there is a record in a different table that is linked to the one
that is to be deleted (i.e. attempting to delete a customer record while
there is invoices that are associated with that customer).
Instead, I was assuming that, just like what happens when you try to delete
a record from within Access, an exception is being raised. This doesn't
happen though - no exception to catch. Even worse, <intRecsAffected> in the
above sample will equal <1>, as if the underlying DELETE statement had been
executed successfully!?
If I do the exact same thing with a SQL Server database, I can catch
<SqlException.Number=547> if the DELETE fails.
Thus, how can I find out as to whether a DELETE was actually successful or
not? There just has to be something other than doing a SELECT after the
DELETE, just to confirm that the record isn't there anymore!?
Also, the same applies to INSERTs and UPDATEs where i.e. a unique index
prevents adding or changing a record ...
Cheers & TIA,
Olaf