Deleting Records in a Database v4.1.6.1

Edit this page

You can use the ExecuteNonQuery() method of EDBCommand to delete records from a database stored on an 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. The following example deletes the employee inserted in the previous example:

<% @ 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 in a web root directory named:

deleteEmployee.aspx

To invoke the sample code, open a web-browser, and browse to:

http://localhost/deleteEmployee.aspx