How to DataAdapter Update Command - OLEDB

The OleDbDataAdapter is a crucial component of the ADO.NET Data Provider, offering essential functionality for data access. It effectively utilizes the Connection object from the .NET Framework data provider to establish a connection with a data source, while employing Command objects to retrieve data from and handle modifications to the data source.

OleDbDataAdapter object

Notably, the InsertCommand, UpdateCommand, and DeleteCommand properties of the OleDbDataAdapter object enable seamless updating of the Data Source with the data modifications executed on a DataSet object. For illustrative purposes, the following C# code snippets exemplify the effective usage of the OleDbDataAdapter object to update an OLEDB Data Source by making use of the properties provided by the UpdateCommand.

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 = "update Users set Password = 'new password' where UserID = 'user1'"; try { connection.Open(); oledbAdapter.UpdateCommand = connection.CreateCommand(); oledbAdapter.UpdateCommand.CommandText = sql; oledbAdapter.UpdateCommand.ExecuteNonQuery(); MessageBox.Show ("Row(s) Updated !! "); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }