C# ADO.NET OleDbDataAdapter

The OleDbDataAdapter is indeed part of the C# ADO.NET Data Provider and is located within the System.Data.OleDb namespace. It facilitates communication between the Dataset and the OleDb Data Sources.

OleDbDataAdapter

When working with the OleDbDataAdapter, it is commonly used in conjunction with the Dataset Object. The DataAdapter provides a mapping mechanism through its Fill() method, which synchronizes the data in the Dataset to match the data in the OleDb Data Source. Conversely, the Update() method of the OleDbDataAdapter updates the data in the OleDb Data Source to match the data in the Dataset. This bidirectional synchronization ensures consistency between the Dataset and the OleDb Data Source.

OleDbDataAdapter oledbAdapter = new OleDbDataAdapter(sql, oledbCnn); oledbAdapter.Fill(ds);

The combination of the OleDbDataAdapter Object and the Dataset Object enables both Data Access and Data Manipulation operations on the OleDb Data Sources. The Fill() method of the DataAdapter retrieves data from the OleDb Data Source and populates the Dataset with the retrieved data. This allows for efficient data access and retrieval.

When the user performs SQL operations like Select or Insert on the data contained in the Dataset Object, it does not directly affect the underlying database until the user explicitly invokes the Update() method on the OleDbDataAdapter. This update operation synchronizes the changes made in the Dataset with the corresponding data in the OleDb Data Source, ensuring that modifications are applied appropriately.

Full Source C#
using System; using System.Windows.Forms; using System.Data; using System.Data.OleDb; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connetionString = null; OleDbConnection oledbCnn ; OleDbDataAdapter oledbAdapter ; DataSet ds = new DataSet(); string sql = null; int i = 0; connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"; sql = "Your SQL Statement Here like Select * from product"; oledbCnn = new OleDbConnection(connetionString); try { oledbCnn.Open(); oledbAdapter = new OleDbDataAdapter(sql, oledbCnn); oledbAdapter.Fill(ds); for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]); } oledbAdapter.Dispose(); oledbCnn.Close(); } catch (Exception ex) { MessageBox.Show("Can not open connection ! "); } } } }

Conclusion

The OleDbDataAdapter Class in the C# ADO.NET Data Provider, located in the System.Data.OleDb namespace, facilitates communication between the Dataset and the OleDb Data Sources. The Fill() method maps the data from the OleDb Data Source to the Dataset, while the Update() method synchronizes changes from the Dataset back to the OleDb Data Source. This combination enables both Data Access and Data Manipulation capabilities, ensuring consistent and efficient interaction between the application and the OleDb Data Source.