DataTable删除SQL Server中未反映的行

时间:2015-05-28 14:00:45

标签: c# sql-server datatable

我是C#的新手,这让我很难过。我的项目使用DataTables和TableAdapters连接到SQL Server数据库。我有一个方法打开Excel,构建一个DataRow,然后将其传递给下面的方法,通过TableAdapter(ctaJETS)将其添加到我的DataTable(cdtJETS)。

    public bool AddJETSRecord(DataRow JETSDataRow)
    {
        bool bolException = false;
        cdtJETS.BeginLoadData();
        // Add the data row to the table
        try
        {
            cdtJETS.ImportRow(JETSDataRow);
        }
        catch (Exception e)
        {
            // Log an exception
            bolException = true;
            Console.WriteLine(e.Message);
        }
        cdtJETS.EndLoadData();
        // If there were no errors and no exceptions, then accept the changes
        if (!cdtJETS.HasErrors && !bolException)
        {
            ctaJETS.Update(cdtJETS);
            return true;
        }
        else
            return false;
    }

以上工作正常,记录按预期显示在SQL Server中。我有另一种方法,抓取该DataTable中的记录子集并将它们输出到另一个Excel文件(这是一个批处理过程,将使用上述方法随时间收集记录,然后偶尔输出它们,所以我不能直接移动从第一个Excel文件到第二个Excel文件的数据。更新第二个Excel文件后,我想删除表中的记录,以便下次运行该方法时不会重复这些记录。这就是我遇到问题的地方:

    public bool DeleteJETSRecords(DataTable JETSData)
    {
        int intCounter = 0;
        DataRow drTarget;
        // Parse all of the rows in the JETS Data that is to be deleted
        foreach (DataRow drCurrent in JETSData.Rows)
        {
            // Search the database data table for the current row's OutputID
            drTarget = cdtJETS.Rows.Find(drCurrent["OutputID"]);
            // If the row is found, then delete it and increment the counter
            if (drTarget != null)
            {
                cdtJETS.Rows.Remove(drTarget);
                intCounter++;
            }
        }

        // Continue if all of the rows were found and removed
        if (JETSData.Rows.Count == intCounter && !cdtJETS.HasErrors)
        {
            cdtJETS.AcceptChanges();
            try
            {
                ctaJETS.Update(dtJETS);
            }
            catch (Exception)
            {
                throw;
            }
            return true;
        }
        else
            cdtJETS.RejectChanges();
            return false;
    }

当我逐步执行该方法时,我可以看到从DataTable中删除的行(即,如果JETSData有10行,最后cdtJETS有n-10行)并且没有抛出异常,但是在我接受更改和更新之后TableAdapter,底层记录仍在我的SQL Server表中。我错过了什么?

1 个答案:

答案 0 :(得分:1)

The Rows.Remove method相当于调用the row's Delete method,后跟the row's AcceptChanges method

the DataTable.AcceptChanges method一样,这表示更改已将保存到数据库中。这不是你想要的。

以下内容应该有效:

public bool DeleteJETSRecords(DataTable JETSData)
{
    int intCounter = 0;
    DataRow drTarget;
    // Parse all of the rows in the JETS Data that is to be deleted
    foreach (DataRow drCurrent in JETSData.Rows)
    {
        // Search the database data table for the current row's OutputID
        drTarget = cdtJETS.Rows.Find(drCurrent["OutputID"]);
        // If the row is found, then delete it and increment the counter
        if (drTarget != null)
        {
            drTarget.Delete();
            intCounter++;
        }
    }

    // Continue if all of the rows were found and removed
    if (JETSData.Rows.Count == intCounter && !cdtJETS.HasErrors)
    {
        // You have to call Update *before* AcceptChanges:
        ctaJETS.Update(dtJETS);
        cdtJETS.AcceptChanges();
        return true;
    }

    cdtJETS.RejectChanges();
    return false;
}