How to DataAdapter Insert Command - OLEDB

The OleDbDataAdapter facilitates communication between the Dataset and the Data Source by utilizing the OleDbConnection Object. The OleDbDataAdapter is equipped with Command objects, namely InsertCommand, UpdateCommand, and DeleteCommand properties, which effectively handle data updates in the data source based on modifications made to the data in the DataSet.

OleDbDataAdapter Object

To insert data into the designated Data Source using the OleDbDataAdapter Object, the following C# Source Code demonstrates the process. Firstly, establish a connection to the Data Source using the OleDbConnection object. Then, create an OleDbCommand object containing the insert SQL statement and assign it to the InsertCommand of the OleDbDataAdapter.

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 = "insert into user values('user1','password1')"; try { connection.Open(); oledbAdapter.InsertCommand = new OleDbCommand(sql, connection); oledbAdapter.InsertCommand.ExecuteNonQuery(); MessageBox.Show ("Row(s) Inserted !! "); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }