Dataadapter DeleteCommand - OLEDB Data Source

The OleDbDataAdapter is a component of the ADO.NET Data Provider that facilitates data access and manipulation. It plays a crucial role in enabling a disconnected data retrieval mechanism in conjunction with the DataSet object. By using the OleDbDataAdapter, we can efficiently retrieve and manipulate data from a data source.

In addition to its data retrieval capabilities, the OleDbDataAdapter also provides the InsertCommand, UpdateCommand, and DeleteCommand properties. These properties represent Command objects that are responsible for managing updates to the specified data source based on modifications made to the DataSet.

To illustrate the usage of the DeleteCommand property, let's consider the following source code example:

Full Source VB.NET
Imports System.Data.OleDb Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connetionString As String Dim connection As OleDbConnection Dim oledbAdapter As New OleDbDataAdapter Dim sql As String connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;" connection = New OleDbConnection(connetionString) sql = "delete from Users where UserID = 'user1'" Try connection.Open() oledbAdapter.DeleteCommand = connection.CreateCommand oledbAdapter.DeleteCommand.CommandText = sql oledbAdapter.DeleteCommand.ExecuteNonQuery() MsgBox("Row(s) Deleted !! ") Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Class

In the above code, we first establish a connection to the database using the provided connection string. We define the select query to retrieve data from the table, as well as the delete query to remove a row from the table based on the ID column. We create an OleDbDataAdapter and set its SelectCommand to the select query.

Next, we create an OleDbCommand for the delete operation and assign it to the DeleteCommand property of the OleDbDataAdapter. We add a parameter to the delete command to specify the ID value for deletion.

To perform the deletion, we populate a DataSet with data from the database using the Fill method of the OleDbDataAdapter. We can then make modifications to the data in the DataSet, such as removing a row.