19.11. Deleting Records in a Database

You can delete records by making use of the SQL Delete command. To execute a delete command with EDB Connector/.NET you need to do the following:

In order to delete the employee that we inserted in the above example we would do something like the following:

Example 19-7. Example - Deleting a Database Record

   
<% @ Page Language="C#" Debug="true"%>
<% @Import Namespace="EnterpriseDB.EDBClient" %>
<% @Import Namespace="System.Data" %>

<script language="C#" runat="server" >

private void Page_Load(object sender, System.EventArgs e)
{
	string strConnectionString = ConfigurationSettings.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 file as "deleteEmployee.aspx" and run it like the other examples. If no errors are generated then a "Record Deleted" message should be shown.

19.11.1. Deleting Records in a Database - Explanation

The delete command is stored in the variable strDeleteQuery. We are passing one parameter to the Delete command, which is the employee number which is specified with ":EmpNo". Then the command is executed with the ExecuteNonQuery() method as we are not returning any values.