using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Duplicate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\Rajesh\\My Documents\\insert1.mdb");
cn.Open();
OleDbCommand cd = new OleDbCommand("SELECT DISTINCT name, MIN(id) AS id FROM duplicate GROUP BY name ORDER BY MIN(id)",cn);
OleDbCommand cdd = new OleDbCommand("select id from duplicate",cn);
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbDataAdapter da1 = new OleDbDataAdapter();
da.SelectCommand = cd;
da1.SelectCommand = cdd;
DataSet ds = new DataSet();
da.Fill(ds,"select1");
da1.Fill(ds,"select2");
for (int j = 0; j < ds.Tables["select2"].Rows.Count; j++)
{
OleDbCommand cd1 = new OleDbCommand("delete from duplicate where id="+ds.Tables["select2"].Rows[j][0]+"", cn);
cd1.ExecuteNonQuery();
}
for (int i = 0; i < ds.Tables["select1"].Rows.Count; i++)
{
OleDbCommand cd2 = new OleDbCommand("insert into duplicate(id,name) values('"+ds.Tables["select1"].Rows[i][1].ToString()+"','"+ds.Tables["select1"].Rows[i][0].ToString()+"')", cn);
cd2.ExecuteNonQuery();
}
dataGridView1.DataSource = ds.Tables["select1"];
}
}
}
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||