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:
using EnterpriseDB.EDBClient; namespace DeletingRecords; internal static class Program { static async Task Main(string[] args) { // NOT FOR PRODUCTION, consider moving the connection string in a configuration file var connectionString = "Server=127.0.0.1;Port=5444;User Id=enterprisedb;Password=edb;Database=edb"; try { await using var dataSource = EDBDataSource.Create(connectionString); await using var conn = await dataSource.OpenConnectionAsync(); await using var deleteCommand = new EDBCommand("DELETE FROM emp WHERE empno = :ID", conn); deleteCommand.Parameters.AddWithValue(":ID", 1234); var numRows = await deleteCommand.ExecuteNonQueryAsync(); Console.WriteLine($"{numRows} record(s) deleted successfully"); await conn.CloseAsync(); } catch (Exception exp) { Console.WriteLine($"An error occured: {exp}"); } } }
using System; using System.Threading.Tasks; using EnterpriseDB.EDBClient; namespace DeletingRecords { internal static class Program { static async Task Main(string[] args) { // NOT FOR PRODUCTION, consider moving the connection string in a configuration file var connectionString = "Server=127.0.0.1;Port=5444;User Id=enterprisedb;Password=edb;Database=edb"; try { using (var dataSource = EDBDataSource.Create(connectionString)) using (var conn = await dataSource.OpenConnectionAsync()) { using (var deleteCommand = new EDBCommand("DELETE FROM emp WHERE empno = :ID", conn)) { deleteCommand.Parameters.AddWithValue(":ID", 1234); var numRows = await deleteCommand.ExecuteNonQueryAsync(); Console.WriteLine($"{numRows} record(s) deleted successfully"); } await conn.CloseAsync(); } } catch (Exception exp) { Console.WriteLine($"An error occured: {exp}"); } } } }
This program should show the following output in the Console:
1 record(s) deleted successfully