Documentation
 
 
 

25.12. Deleting Records in a Database

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

  • Creating and opening a database connection

  • Creating a database command that represents the delete statement to be executed

  • Execute the delete command using the ExecuteNonQuery() method of EDBCommand.

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

Example 25-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.

25.12.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.

 
 ©2004-2007 EnterpriseDB All Rights Reserved