如何根据另一个表删除一个表中的行?
我有一个 table1,其中包含我想从 table2 中删除的数据行。我尝试循环遍历数据并相应地删除...
while (myDataReader.Read())
{
DataTable.Rows.Remove(DataRow);
}
不走运,我也尝试在填充两个表后删除
var correctDataTable = from p in DataTable
where (!tempDataTable.Rows.Contains(p))
select new { p };
任何想法吗?
I have a table1 with datarows that I would like to remove from table2. I tried to loop thru the data and remove accordingly...
while (myDataReader.Read())
{
DataTable.Rows.Remove(DataRow);
}
no luck, I also tried to remove after the two tables have been populated
var correctDataTable = from p in DataTable
where (!tempDataTable.Rows.Contains(p))
select new { p };
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会编写一个查询来在一个语句中完成这一切
I would write a query to do it all in one statement
Hold on
实际上不会删除数据库中的数据,假设这是您想要的。
这样,您只需从数据表对象的行集合中删除行。
下一步是将更改实际提交到数据库。再次假设这就是您正在寻找的东西。
Hold on
will not actually remove the data in your database, supposing this is what you want.
This way you only remove rows from a rows collection in you datatable object.
Next step is to actually commit the changes to the database. Again, supposing this is what you are looking for.
你说的删除是什么意思?您能具体说明一下您想要做什么吗?
要从表格中删除整行,还是仅删除一个单元格? - 基于什么?
你的第一篇文章非常不清楚。
所以你有两张桌子。第一个拥有所有数据,第二个拥有一些数据。如果表2中的任何数据存在于表1中,那么表1中的这些数据必须被删除吗?我说得对吗?
您可以循环遍历两个表的行(因此双循环):
What do you mean to remove? Can you please specify a bit better what would you like to to?
To remove the whole row from the table, or only one cell? - based on what?
Your 1st post is very unclear.
So you have two tables. 1st one has ALL the data, the 2nd one has some. And if any data from table2 exists in table1, these data from table 1 has to be removed? Am I right?
You can loop through the rows of both tables (so double loop):
我想通了。我将以下内容添加到 table2(要删除行的表)的 while 循环中。
I figured it out. I added the following to my while loop for table2(table with rows to be removed).
如果您想要使用存储在另一个数据表中的键从数据表中删除一些行,您可以使用键迭代表,在表中找到要删除的键并…完成。
示例:
考虑具有 (table1ID, col2, col3, col4) 列的 TABLE2 和具有 (id, col2, col3) 列的 TABLE1。
您可以这样做:
这是考虑到您使用带有主键的类型化数据集。
If what you want is to delete some rows from a dataTable using a key stored in another dataTable you can iterate the table with the keys, find the key in the table where you want to delete and …done.
Example:
Consider TABLE2 with (table1ID, col2, col3, col4) columns and TABLE1 with (id, col2, col3) columns.
You can do:
This is considering that you use Typed datasets with primary keys.
从 dtCMATM 表中删除 dtAllowedList 表中没有的所有记录。
Remove all records from dtCMATM table that are not in dtAllowedList table.