DataTable 删除未反映在 SQL 服务器中的行
DataTable Remove Rows Not Reflected in SQL Server
我对 C# 还很陌生,这让我很困惑。我的项目正在使用 DataTables 和 TableAdapters 连接到 SQL 服务器数据库。我有一个方法可以打开 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 服务器中。我有另一种方法可以获取该 DataTable 中的记录子集并将它们输出到另一个 Excel 文件(这是一个批处理过程,它将使用上述方法随着时间的推移收集记录,然后偶尔输出它们,所以我可以'直接将数据从第一个 Excel 文件移动到第二个)。在更新第二个 Excel 文件后,我想从 table 中删除记录,以便下次方法为 运行 时它们不会重复。这是我遇到问题的地方:
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服务器table里。我错过了什么?
The Rows.Remove
method is equivalent to calling the row's Delete
method, followed by 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;
}
我对 C# 还很陌生,这让我很困惑。我的项目正在使用 DataTables 和 TableAdapters 连接到 SQL 服务器数据库。我有一个方法可以打开 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 服务器中。我有另一种方法可以获取该 DataTable 中的记录子集并将它们输出到另一个 Excel 文件(这是一个批处理过程,它将使用上述方法随着时间的推移收集记录,然后偶尔输出它们,所以我可以'直接将数据从第一个 Excel 文件移动到第二个)。在更新第二个 Excel 文件后,我想从 table 中删除记录,以便下次方法为 运行 时它们不会重复。这是我遇到问题的地方:
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服务器table里。我错过了什么?
The Rows.Remove
method is equivalent to calling the row's Delete
method, followed by 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;
}