Computing.Net > Forums > Database > Removal of duplicate rows

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Removal of duplicate rows

Reply to Message Icon

Name: access
Date: October 12, 2008 at 12:41:30 Pacific
OS: windows xp
CPU/Ram: core 2 dual, 2 GB
Product: Dell Notebook
Comment:

hi
i have a window application, wherein i have to choose the database,its table and rowfields present in the table at runtime. Now i want to remove all the duplicates from database. my database is access and whichever row field the user chooses the duplicates should be removed leaving the latest updated row. Please help me my code is as below..
string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtPath.Text;
OleDbConnection con = new OleDbConnection(str);

string t = "select * from " + cmbtables.SelectedItem.ToString();
OleDbDataAdapter adt = new OleDbDataAdapter(t, str);
con.Open();
DataSet ds = new DataSet();


adt.Fill(ds);

DataTable dt = ds.Tables[0];
con.Close();
dt = RemoveDuplicateRows(dt, cmbRow.SelectedItem.ToString());


private DataTable RemoveDuplicateRows(DataTable dTable, string colName)
{
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();
foreach (DataRow drow in dTable.Rows)
{
if (hTable.Contains(drow[colName]))
duplicateList.Add(drow);
else
hTable.Add(drow[colName], string.Empty);
}
foreach (DataRow dRow in duplicateList)
dTable.Rows.Remove(dRow);
return dTable;
}
when i use datatable.update it is giving me error
hoping for your co-operation
Thank you



Sponsored Link
Ads by Google

Response Number 1
Name: mbshinde78
Date: October 15, 2008 at 09:40:01 Pacific
Reply:

Hi,

You can achieve that using DISTINCT keyword which will remove duplicate rows.

Manoj Shinde
Useful Softwares


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Database Forum Home


Sponsored links

Ads by Google


Results for: Removal of duplicate rows

Swap contend of 2 rows in a table www.computing.net/answers/dbase/swap-contend-of-2-rows-in-a-table/355.html

DSN Configuration - SQL Server www.computing.net/answers/dbase/dsn-configuration-sql-server/250.html

Beginner Question - Duplicate Entries www.computing.net/answers/dbase/beginner-question-duplicate-entries/637.html