C# Dataset with multiple tables - OLEDB
The DataSet contains DataTableCollection and their DataRelationCollection . The DataTableCollection contains zero or more DataTable objects. The OleDbDataAdapter object allows us to populate DataTables in a DataSet. We can use Fill method in the OleDbDataAdapter Object for populating data in a Dataset.
ADO.NET enables you to create DataTable objects and add them to an existing DataSet. In a DataSet with multiple DataTable Objects , you can use DataRelation objects to relate one table to another, to navigate through the tables, and to return child or parent rows from a related table. We can populate Dataset with more than one table at a time using OleDbDataAdapter Object . The following C# source code shows how to a single OleDbDataAdapter fill Dataset with multiple tables.
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 ; DataSet ds = new DataSet(); string firstSql = null; string secondSql = null; int i = 0; connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"; firstSql = "Your First SQL Statement Here"; secondSql = "Your Second SQL Statement Here"; connection = new OleDbConnection(connetionString); try { connection.Open(); oledbAdapter = new OleDbDataAdapter(firstSql, connection); oledbAdapter.Fill(ds, "First Table"); oledbAdapter.SelectCommand.CommandText = secondSql; oledbAdapter.Fill(ds, "Second Table"); oledbAdapter.Dispose(); connection.Close(); //retrieve first table data 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]); } //retrieve second table data for (i = 0; i <= ds.Tables[1].Rows.Count - 1; i++) { MessageBox.Show(ds.Tables[1].Rows[i].ItemArray[0] + " -- " + ds.Tables[1].Rows[i].ItemArray[1]); } } catch (Exception ex) { MessageBox.Show("Can not open connection ! "); } } } }
- What is C# ADO.NET Dataset
- C# Datset with Sql Server Data Provider
- C# Datset with OLEDB Data Provider
- Find Tables in a Dataset - Sql Server
- Find Tables in a Dataset - OLEDB
- How to Dataset rows count - Sql Server
- How to Dataset rows count - OLEDB
- How to find Column Definition SqlServer
- How to find Column Definition OLEDB
- How to Dyanamic Dataset in C#
- C# Dataset with multiple tables - Sql Server
- C# Dataset table relations
- C# Dataset merge tables - Sql Server
- C# Dataset merge tables - OLEDB
- Bind a dataset to a combo box in C#, bind enum to combobox , bind dictionary to combobox
- How to find tables in a Database in C#