Merge Case 2: Same Table Structures,No Primary Key
- In the strongly typed DataSet, Table0 can be used as a sample table that has no primary keys
defined on it.
- Binding the student datasource in table1 and table2
- write the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable ftb = new DataTable();
DataTable stb = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
ftb.Columns.Add("Eno", typeof(Int32));
ftb.Columns.Add("Ename", typeof(String));
ftb.PrimaryKey = new DataColumn[] { ftb.Columns["Eno"] };
ftb.LoadDataRow(new object[] { "1", "Ashish" }, true);
ftb.LoadDataRow(new object[] { "2", "KAMAL" }, true);
stb.Columns.Add("Eno", typeof(Int32));
stb.Columns.Add("Ename", typeof(String));
stb.LoadDataRow(new object[] { "2", "Aditya" }, true);using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the
//'studentDataSet.student2' table.
You can move, or remove it, as needed.
this.student2TableAdapter.Fill(this.studentDataSet.student2);
// TODO: This line of code loads data into the
//'studentDataSet.std1' table.
You can move, or remove it, as needed.
this.std1TableAdapter.Fill(this.studentDataSet.std1);
}
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.
4.0;Data Source=F:\\student.mdb");
OleDbDataAdapter ad = new OleDbDataAdapter("select * from std1", con);
DataTable tb1 = new DataTable();
ad.Fill(tb1);
//dataGridView4.Visible = true;
//dataGridView4.DataSource = tb1;
OleDbDataAdapter ad1 = new OleDbDataAdapter("select * from student2", con);
DataTable tb2 = new DataTable();
ad1.Fill(tb2);
tb2.Merge(tb1);
dataGridView3.DataSource = tb2;
dataGridView3.Visible = true;
dataGridView1.Visible = false;
dataGridView2.Visible = false;
label1.Visible = false;
label2.Visible = false;
}
}
}
stb.LoadDataRow(new object[] { "3", "Anil" }, true);
dataGridView1.DataSource = ftb;
dataGridView2.DataSource = stb;
}
private void button1_Click(object sender, EventArgs e)
{
ftb.Merge(stb);
dataGridView1.DataSource=ftb;
dataGridView2.Visible = false;
label1.Text = "After Merging";
label2.Visible = false;
}
}
}
- Run thr program
- click the merge button