简体   繁体   English

如何根据另一个表删除一个表中的行?

[英]How do I remove a row in one table based on another table?

I have a table1 with datarows that I would like to remove from table2. 我有一个table1与datarows我想从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? 有任何想法吗?

I would write a query to do it all in one statement 我会写一个查询来在一个语句中完成所有操作

delete from TableA
where ColID in (select colid from tableB)

Remove all records from dtCMATM table that are not in dtAllowedList table. 从dtCMATM表中删除不在dtAllowedList表中的所有记录。

     private void GetProperData()
   {
        DataTable dtAllowedList = someclass.somefunctiontogetdata(....);
        DataTable dtCMATM = someclass.somefunctiontogetdataTobeRemovedLaterOn(.........);
        DataTable tblCloned = new DataTable();
        tblCloned = dtCMATM.Clone();

        foreach (DataRow dr2 in dtAllowedList.Rows)
        {
        DeleteRowsFromDataTable(ref tblCloned, dtCMATM, "AssignTo", dr2[0].ToString());
        }

        tblCloned.AcceptChanges();
        dtCMATM = tblCloned;
        dtCMATM.AcceptChanges();    

   }

    private static void DeleteRowsFromDataTable(ref DataTable tblCloned, DataTable dtCMATM, string ColumnName, string columnValue)
    {           
        string strExpression = "AssignTo = '" + columnValue + "' ";
        dtCMATM.DefaultView.RowFilter = strExpression;
        dtCMATM = dtCMATM.DefaultView.ToTable();

        if (dtCMATM.Rows.Count > 0)
        {
            tblCloned.ImportRow(dtCMATM.Rows[0]);
        }           
    }    

Hold on 坚持,稍等

 DataTable.Rows.Remove(DataRow); 

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. 再次,假设这是你正在寻找的。

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? 如果table1中存在table2中的任何数据,那么必须删除表1中的这些数据? Am I right? 我对吗?

You can loop through the rows of both tables (so double loop): 你可以循环遍历两个表的行(所以双循环):

foreach (DataRow dr1 in table1.Rows)
        {
            foreach (DataRow dr2 in table2.Rows)
            {
                if (dr1[0].ToString() == dr2[0].ToString())
                {
                    table1.Rows.Remove(dr1);
                    break;
                }
            }
        }

I figured it out. 我想到了。 I added the following to my while loop for table2(table with rows to be removed). 我将以下内容添加到table2的while循环中(包含要删除的行的表)。

while (myDataReader.Read())
{
     DataRow drNew = DataTable.NewRow();
     drNew["ID"] = myDataReader["ID#"].ToString().Trim();
     drNew["Name"] = myDataReader["NAME"].ToString().Trim());
     ...
     DataRow[] badRow = DataTable.Select(
          "ID='" + drNew["ID"] + "' and Name='" + drNew["Name"] + "'");
     if(badRow.Length >0)
         DataTable.Rows.Remove(badRow[0]);
}

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. 如果您想要使用存储在另一个dataTable中的密钥从dataTable中删除某些行,您可以使用键迭代表,在表中找到要删除的键并完成。

Example: 例:

Consider TABLE2 with (table1ID, col2, col3, col4) columns and TABLE1 with (id, col2, col3) columns. 考虑带有(table1ID,col2,col3,col4)列的TABLE2和带有(id,col2,col3)列的TABLE1。 You can do: 你可以做:

foreach (TABLE2row t2r in TABLE2)
{
    t1r=TABLE1.FindByID(t2r.Table1ID);
    if (t1r!=null) 
    {
        t1r.delete();
    }
}

This is considering that you use Typed datasets with primary keys. 这是考虑您使用带主键的Typed数据集。

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

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