我试图从Main表中删除重复项。
看起来像这样(这里用csv格式):
"Record ID";Status;Placement;Private;Category;Note;Blob
例如
14341692;132;2147483647;False;4;"29.12.10 14:17";System.Byte[]
重复意味着,注意是相同的。我的方法是:
string strSQL = "SELECT * FROM Main";
OleDbCommand cmd = new OleDbCommand(strSQL, MemoVerbindung);
OleDbDataReader dr = cmd.ExecuteReader();
_items = new List<string>(); // <-- Add this
while (dr.Read())
{
if (dr.FieldCount >= 5)
_items.Add(dr[5].ToString());
}
dr.Close();
progressBar1.Maximum = _items.Count;
for (int a = 0; a < _items.Count; a++)
{
progressBar1.Value = a;
strSQL = "SELECT * FROM Main WHERE Note = '" + _items[a].ToString().Replace("'", "\'") + "'";
cmd = new OleDbCommand(strSQL, MemoVerbindung);
dr = null;
OleDbDataReader dr2 = null;
try
{
dr = cmd.ExecuteReader();
int u = 0;
if (dr.FieldCount > 1)
{
while (dr.Read())
{
if (u >= 1)
{
string was = "DELETE FROM Main WHERE [Record ID] = " + dr[0];
OleDbCommand command = new OleDbCommand(was, MemoVerbindung);
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//MessageBox.Show(reader[0].ToString());
}
reader.Close();
}
u++;
}
}
else
{
while (dr.Read())
{
MessageBox.Show("Übrig");
}
}
dr.Close();
}
catch (Exception mm)
{
//MessageBox.Show(mm.Message);
}
}
MessageBox.Show("Fertig");
progressBar1.Value = 0;
所以在if(u&gt; = 1)部分,我试图留下一个版本,同时删除所有其他版本。不幸的是,这不起作用意味着所有条目都被删除,但出于某种原因引发错误的条目。你会改变什么,或者通常更优雅的方式?
答案 0 :(得分:0)
使用此SQL语句查找重复项:
SELECT First(Main.[Note]) AS [NoteField], Count(Main.[Note]) AS NumberOfDups
FROM Main
GROUP BY Main.[Note]
HAVING (((Count(Main.[Note]))>1));
然后你可以遍历这个记录集(把它作为SnapShot抓取,这样对基础数据的更改不会改变结果)
使用!NoteField
知道要查找的注释,!NumberOfDups
知道要删除的数量(删除此数字 - 1)