SiteMap  | About    

C# Dataset merge tbales - OLEDB

The DataSet Object contains copy of the data we requested through the SQL statement. The OleDbDataAdapter object allows us to populate DataTable in a DataSet. We can use Fill method in the OleDbDataAdapter for populating data in a Dataset. We can populate Dataset with more than one table at a time using OleDbDataAdapter Object. The DataTableCollection Object contains zero or more DataTable objects.

A DataSet Object represents a complete set of data including the tables that contain, order, and constrain the data, as well as the relationships between the tables. In some situation we want to combine the result of multiple SQL query results as a single result set. In that case we can use the Dataset's Merge method for doing this. The tables involved in the merge should be identical. The following C# source code shows how to merge the tables from two different dataset.

using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace WindowsApplication1
    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
            string connetionString = null;
            OleDbConnection connection ;
            OleDbDataAdapter oledbAdapter ;
            DataSet ds1 = new DataSet();
            DataSet ds2 = new DataSet();
            DataTable dt ;
            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);
                oledbAdapter = new OleDbDataAdapter(firstSql, connection);
                oledbAdapter.Fill(ds1, "First Table");
                oledbAdapter.SelectCommand.CommandText = secondSql;
                oledbAdapter.Fill(ds2, "Second Table");

                dt = ds1.Tables[0];

                for (i = 0; i <= dt.Rows.Count - 1; i++)
                    MessageBox.Show(dt.Rows[i].ItemArray[0] + " -- " + dt.Rows[i].ItemArray[0]);

            catch (Exception ex)
                MessageBox.Show("Can not open connection ! ");
} (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.