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

发布于 2024-10-28 22:10:23 字数 374 浏览 1 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(6

轻许诺言 2024-11-04 22:10:23

我会编写一个查询来在一个语句中完成这一切

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

I would write a query to do it all in one statement

delete from TableA
where ColID in (select colid from tableB)
待天淡蓝洁白时 2024-11-04 22:10:23

Hold on

 DataTable.Rows.Remove(DataRow); 

实际上不会删除数据库中的数据,假设这是您想要的。
这样,您只需从数据表对象的行集合中删除行。

下一步是将更改实际提交到数据库。再次假设这就是您正在寻找的东西。

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.

零崎曲识 2024-11-04 22:10:23

你说的删除是什么意思?您能具体说明一下您想要做什么吗?
要从表格中删除整行,还是仅删除一个单元格? - 基于什么?
你的第一篇文章非常不清楚。

所以你有两张桌子。第一个拥有所有数据,第二个拥有一些数据。如果表2中的任何数据存在于表1中,那么表1中的这些数据必须被删除吗?我说得对吗?

您可以循环遍历两个表的行(因此双循环):

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

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):

foreach (DataRow dr1 in table1.Rows)
        {
            foreach (DataRow dr2 in table2.Rows)
            {
                if (dr1[0].ToString() == dr2[0].ToString())
                {
                    table1.Rows.Remove(dr1);
                    break;
                }
            }
        }
千紇 2024-11-04 22:10:23

我想通了。我将以下内容添加到 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]);
}

I figured it out. I added the following to my while loop for table2(table with rows to be removed).

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]);
}
天涯沦落人 2024-11-04 22:10:23

如果您想要使用存储在另一个数据表中的键从数据表中删除一些行,您可以使用键迭代表,在表中找到要删除的键并…完成。

示例

考虑具有 (table1ID, col2, col3, col4) 列的 TABLE2 和具有 (id, col2, col3) 列的 TABLE1。
您可以这样做:

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

这是考虑到您使用带有主键的类型化数据集。

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:

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.

瞎闹 2024-11-04 22:10:23

从 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]);
        }           
    }    

Remove all records from dtCMATM table that are not in dtAllowedList table.

     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]);
        }           
    }    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文