ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
ASP Dot Net Tutorial - Index
ASP Dot Net Tutorial - Microsofts ASP.NET
ASP Dot Net Tutorial - Begining Web Forms
ASP Dot Net Tutorial - User Controls
ASP Dot Net Tutorial - ADO .NET
ASP Dot Net Tutorial - DataBinding to WebControls
ASP Dot Net Tutorial - DataList Controls
ASP Dot Net Tutorial - DataGrid Control
ASP Dot Net Tutorial - Advance Practical Examples on DataGrid
ASP Dot Net Tutorial - Working with DataSets
ASP Dot Net Tutorial - Working with XML
ASP Dot Net Tutorial - Forms Based Authentication
ASP Dot Net Tutorial - Windows Based Authentication
     

 
Online Personal Trainer Certification
Test Search by Job Title
Find a Personal Trainer
Job Resources
Free Self Tests and Quizzes
Find a Lawyer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Six Sigma Green Belt Certification
Lean Management Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
SEO Certification
Online Photography Course & Certification
Online Business Writing Course & Certification
Baby Sitting Course & Certification
Time Manangement Certification
Health Club Management Certification
Selling Skills Certification
Forensic Skills Certification
Green Living Course  free!
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > ASP.NET Tutorial > ADO.NET
 

ASP.NET Tutorial - ADO.NET

                                                                                                             Page 12 of 14
 

Deleting Database Records

A data from a database is deleted by using the SQL Delete statement. The syntax for a Delete statement is as follows: -

DELETE table_name WHERE condition

If, for example, you want to delete all the rows from a table named Books where the bookName column has the value ASP.NET; the following statement would be used.

DELETE Books WHERE bookName ='ASP.NET'

For the use of DELETE command follow these steps: -

1.Create and open a database connection.

2.Create a database command that represents the SQL Delete statement to execute.

3.Execute the command by calling the ExecuteNonQuery () method.

Look at the following example for executing DELETE command: -

 

Example 33 SqlDeleteDemo.aspx

<%@ Import Namespace="System.Data.SqlClient" %>

<%

Dim conNorthwind As SqlConnection

Dim strDelete As String

Dim cmdDelete As SqlCommand

conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;database=Northwind" )

strDelete = "Delete Books Where bookName='ASP.NET'"

cmdDelete = New SqlCommand( strDelete, conNorthwind )

conNorthwind. Open ()

cmdDelete. ExecuteNonQuery ()

conNorthwind. Close ()

%>
 

Record Deleted from the table Books!

The output of this example is shown below:

 

The first line in the above example imports the necessary namespace to work with SQL Server.

Next, a connection to the SQL Server running on the local machine is initialized.

The SqlCommand class is initialized with two parameters: a SQL Delete statement and an instance of the SqlConnection class. Next, the connection is opened, the command is executed by calling ExecuteNonQuery (), and the connection is closed.

Next example shows how the same task can be accomplished in Microsoft Access database table named Books using the System.Data.OleDb namespace.

Example 34 OleDbDeleteDemo.aspx

<%@ Import Namespace="System.Data.OleDb" %>

<%

Dim conNorthwind As OleDbConnection

Dim strDelete As String

Dim cmdDelete As OleDbCommand

 

conNorthwind = New OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=c:\Books.mdb")

strDelete = "Delete from Books Where bookName='ASP.NET'"

cmdDelete = New OleDbCommand( strDelete, conNorthwind )

conNorthwind. Open ()

cmdDelete. ExecuteNonQuery ()

conNorthwind. Close ()

%>

Records Deleted from the table Books!

 

 

The output is shown as below:-

The first line in above example imports the necessary namespace to work with MS Access.

Next, a connection to the MS ACCESS running on the local machine is initialized.

The OleDbCommand class is initialized with two parameters: a SQL Delete statement and an instance of the OleDbConnection class.

Next, the connection is opened, the command is executed by calling ExecuteNonQuery (), and the connection is closed.

Important: - You must use Delete From rather than just Delete when working with a Microsoft Access database.

Parametric Delete

Practically we use Parametric Delete in the form of a web i.e. the parameters are passed from outside. The following example demonstrates it: -

 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2006. All Rights Reserved.