How to DataAdapter Delete Command - OLEDB

The OleDbDataAdapter plays a crucial role as part of the ADO.NET Data Provider, offering essential functionality for data management. Specifically, the OleDbDataAdapter collaborates with the DataSet to provide a robust mechanism for retrieving data in a disconnected manner. This approach ensures efficient handling of data without requiring a constant connection to the data source.

To facilitate this process, the OleDbDataAdapter utilizes the Connection object from the .NET Framework data provider to establish a connection with the designated data source. Additionally, it employs Command objects to effectively retrieve data from the data source and handle any necessary changes or modifications.

Command objects

The InsertCommand, UpdateCommand, and DeleteCommand properties of the DataAdapter serve as essential Command objects responsible for managing updates to the data in the data source. These properties diligently execute modifications made to the data in the DataSet, ensuring accurate synchronization with the data source.

To exemplify the implementation of the DeleteCommand property within the OleDbDataAdapter for data deletion in a database, the following C# Source Code demonstrates the necessary steps to perform this operation.

Full Source C#
using System; using System.Data; using System.Data.OleDb; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connetionString = null; OleDbConnection connection ; OleDbDataAdapter oledbAdapter = new OleDbDataAdapter(); string sql = null; connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"; connection = new OleDbConnection(connetionString); sql = "delete from tblusers1 where UserID = 'user1'"; try { connection.Open(); oledbAdapter.DeleteCommand = connection.CreateCommand(); oledbAdapter.DeleteCommand.CommandText = sql; oledbAdapter.DeleteCommand.ExecuteNonQuery(); MessageBox.Show ("Row(s) Deleted !! "); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }