将已删除的行保存在数据表中以便稍后在数据库中删除

发布于 2024-10-03 10:51:58 字数 564 浏览 1 评论 0原文

抱歉,如果标题有点含糊,但我现在正在想一个好方法来做到这一点。顺便说一句,我们正在使用 .NET 3.5。

示例代码:

if (ExistsInDB(dt.Rows.Find(rowID)))
     dt.Rows.Find(rowID).Delete();
else
     dt.Rows.Remove(dt.Rows.Find(rowID));

如果数据库中存在该行,则将其标记为删除。否则,将其从数据表中删除。

现在,如果我继续通过 DataRow.Delete() 方法将 DataRow 的 RowState 属性更改为已删除,那么效果很好,除非我无法访问信息,当用户点击“保存更改”时,我需要从数据库中删除该行”。

我不想将行存储在另一个数据表中,因为我已经有很多其他数据表,并且我不想给对象添加更多复杂性。我会在数据表上放置另一列,名称为“ToBeDeleted”或其他名称,但表通过网格显示在程序中。

本质上,我希望能够标记 DataTable 中的 DataRow 以便稍后在数据库中删除,而无需将 DataRow 放入列表中或向其中添加另一列。这可能吗?

Sorry if the title is a bit vague, but I'm trying to think of a good way to do this right now. We are using .NET 3.5, by the way.

Example code:

if (ExistsInDB(dt.Rows.Find(rowID)))
     dt.Rows.Find(rowID).Delete();
else
     dt.Rows.Remove(dt.Rows.Find(rowID));

If the row exists in the database, mark it for deletion. Otherwise, remove it from the datatable.

Now, if I go ahead and change the DataRow's RowState property to Deleted via the DataRow.Delete() method, that works fine except I can't access information I would need to delete the row from the database when the user hits "Save Changes".

I don't want to store the rows in another datatable because I already have a lot of other data tables and I don't want to add more complexity to the object. I would put another column on the datatable with the name "ToBeDeleted" or something, but the tables get displayed in the program via a grid.

Essentially, I want to be able to flag a DataRow in a DataTable for deletion in the database later without resorting to throwing the DataRow into a List or adding another column to it. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮凉 2024-10-10 10:51:58

您可以通过以下方式获取已删除行的数据

row["ColumnName", DataRowVersion.Original]

You can get the deleted row's data by writing

row["ColumnName", DataRowVersion.Original]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文