将已删除的行保存在数据表中以便稍后在数据库中删除
抱歉,如果标题有点含糊,但我现在正在想一个好方法来做到这一点。顺便说一句,我们正在使用 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过以下方式获取已删除行的数据
You can get the deleted row's data by writing
为什么不使用 DataAdapter 呢?您可以在 http://msdn.microsoft.com/en-us/library/xzb1zw3x%28v=VS.90%29.aspx" rel="nofollow">http:// /msdn.microsoft.com/en-us/library/xzb1zw3x%28v=VS.90%29.aspx
谢谢,
瓦米普
Why not use DataAdapter for this? You can learn how to do this at http://msdn.microsoft.com/en-us/library/xzb1zw3x%28v=VS.90%29.aspx
Thanks,
Vamyip