Deleting records in a database v7.0.6.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 strDeleteQuery. The code passes the employee number specified by EmpNo to the DELETE command. The command is then executed using the ExecuteNonQuery() method.

<% @ Page Language="C#" Debug="true"%>
<% @Import Namespace="EnterpriseDB.EDBClient" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Configuration" %>
<script language="C#" runat="server" >
private void Page_Load(object sender, System.EventArgs e)
{
	var strConnectionString = ConfigurationManager.AppSettings["DB_CONN_STRING"];
	var strDeleteQuery  = "DELETE FROM emp WHERE empno = :ID";
	try
	{
		await using var dataSource = EDBDataSource.Create(ConnectionString);
		var conn = await dataSource.OpenConnectionAsync();
		var strDeleteQuery = "DELETE FROM emp WHERE empno = :ID";
		using var deleteCommand = new EDBCommand(strDeleteQuery, conn);
		deleteCommand.Parameters.Add(new EDBParameter(":ID", EDBTypes.EDBDbType.Integer));
		deleteCommand.Parameters[0].Value = 1234;
		await deleteCommand.ExecuteNonQueryAsync();
		Response.Write("Record Deleted");
		await conn.CloseAsync();
	}
	catch(Exception exp)
	{
		Response.Write(exp.ToString());
	}
}
</script>

Save the sample code in a file named deleteEmployee.aspx in a web root directory.

To invoke the sample code, enter the following in a browser: http://localhost/deleteEmployee.aspx