简体   繁体   English

c#DataSet有2个表,如何从一个表中删除一行,它将从另一个表中删除一行?

[英]c# DataSet with 2 tables, how to delete a row from one it will delete a row from the other?

I use Visual Studio 2010 我使用Visual Studio 2010

I have a DataSet with 2 tables 我有2个表的数据集

one (MainList) has type, name, path, parameter the other (UpadteList) has path, hash, date 一个(MainList)具有类型,名称,路径,参数,另一个(UpadteList)具有路径,哈希,日期

I added files to this list and it work with out problem, now I have the following 我将文件添加到此列表中,并且可以正常使用,现在有以下内容

when I add file type "update" it will be "Update","My Program", "PATH-TO-/my.setup.exe","/minimized" 当我添加文件类型“更新”时,它将是“更新”,“我的程序”,“ PATH-TO- / my.setup.exe”,“ / minimized”

if it was type "Update" the following data goes to (UpdateList) "PATH-TO-/my.setup.exe","asdfwefwfgg3r34t34t34t","2010-09-01" 如果是类型“ Update”,则以下数据将转到(UpdateList)“ PATH-TO- / my.setup.exe”,“ asdfwefwfgg3r34t34t34t”,“ 2010-09-01”

I want when a row in the (MainList) deleted and a row with the same path in (UpdateList) exsist it will deleted too 我想要删除(MainList)中的行并且(UpdateList)中具有相同路径的行也将被删除时

should I use loop or (I saw) in dataSet properties use Relations 我应该在dataSet属性中使用循环还是(我看到)使用Relation

what is the best approach ? 最好的方法是什么?

with loops I got into some ugly bugs that delete everything in the Mainlist! 通过循环,我遇到了一些丑陋的错误,这些错误删除了Mainlist中的所有内容!

note: I use XML to store data (the data is not big) 注意:我使用XML来存储数据(数据不大)

IMO, you have two choices 海事组织,你有两个选择

  1. Use DataTable constraints such as ForeignKeyConstraint with DeleteRule=Cascade settings. 使用DataTable约束(例如ForeignKeyConstraint和DeleteRule = Cascade设置)。
  2. Use RowDeleted /RowDeleting event on MainList data-table to look up and delete the corresponding row in other table. 在MainList数据表上使用RowDeleted / RowDeleting事件可查找和删除其他表中的相应行。

I think that code is arguably the wrong place to deal with data consistency. 我认为代码可以说是处理数据一致性的错误位置。 I'd do it in the database, either in a stored procedure or, if you're using SQL Server, possibly with cascading deletes . 我会在数据库中执行此操作,或者在存储过程中执行此操作,或者如果您使用的是SQL Server,则可能需要级联deletes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM