Deleting records in a database v5.0.7.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 to the DELETE command (specified by EmpNo). 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)
{
  string strConnectionString = ConfigurationManager.AppSettings
  ["DB_CONN_STRING"];

  EDBConnection conn = new EDBConnection(strConnectionString);

  string strDeleteQuery  = "DELETE FROM emp WHERE empno = :ID";

  try
{
      conn.Open();

      EDBCommand deleteCommand = new EDBCommand(strDeleteQuery,conn);

      deleteCommand.Parameters.Add
      (new EDBParameter(":ID", EDBTypes.EDBDbType.Integer));

      deleteCommand.Parameters[0].Value = 1234;

      deleteCommand.ExecuteNonQuery();

      Response.Write("Record Deleted");

  }

  catch(Exception exp)
{

      Response.Write(exp.ToString());

  }

  finally
{

      conn.Close();

  }
}
</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