Deleting records in a database v9.0.3.1

You can use the ExecuteNonQuery() method of EDBCommand to delete records from a database stored on an EDB Postgres Advanced Server host with a DELETE statement.

In the example that follows, the DELETE command is stored in the variable deleteCommand. The values prefixed with a colon (:) are placeholders for EDBParameters.

The EDBParameter for the employee ID is created and assigned at the same time using command’s parameter collection EDBParameterCollection.AddWithValue(string parameterName, object value) method.

The DELETE command is then executed by the ExecuteNonQuery() method of the deleteCommand object.

Note that ExecuteNonQuery() method returns the number of rows affected by the command. It is usually a good practice to check that the number of affected rows matches your expectations (0 or 1 in this example).

The example deletes an employee having the 1234 ID from the emp table:

This program should show the following output in the Console:

1 record(s) deleted successfully