1

我试图从主表中删除重复项。

它看起来像这样(此处为 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 >= 1) 部分中,我试图保留一个版本,同时删除所有其他版本。不幸的是,这不起作用意味着所有条目都被删除,但由于某种原因引发错误的条目。你会改变什么或者有一个更优雅的方式?

4

1 回答 1

0

使用此 SQL 语句查找重复项:

SELECT First(Main.[Note]) AS [NoteField], Count(Main.[Note]) AS NumberOfDups
FROM Main
GROUP BY Main.[Note]
HAVING (((Count(Main.[Note]))>1));

然后您可以遍历此记录集(将其作为快照获取,因此对基础数据的更改不会更改结果)
用于!NoteField知道要查找的注释,并!NumberOfDups知道您必须删除多少(删除此数字 - 1)

于 2012-06-19T13:22:37.093 回答