首页 文章

C#MYSQL在另一个DataTable中找到DataTable.PrimaryKey?

提问于
浏览
0

我有2个DataTables with Data(dtm和dtl) . 我必须知道在Datatable2中是否存在来自DataTable1的DataRow . 我有一个3列的组合初始键 . 我知道我可以像这样得到DataTable主键 .

DataColumn[] pkcol;
pkcol = dtm.PrimaryKey;

是否可以像这样使用Find方法?

if (dtl.Rows.Find(dtm[pkcol]) == null)
   {

   }

我必须实现DataTable Sync方法 . 所以我在dtl.Rows中的dtm.Rows和Foreach Datarow中找到了Datarow . 如果我可以继续在表上搜索是否存在表中的datarows主键值,那将是很好的 . 有任何想法吗?请帮忙 . 谢谢

1 回答

  • 0

    根据MSDN DataRowCollection.Find 已经在寻找PK中具有给定值的行 . 所以你甚至不需要获取PK,而只需要一个值数组,它们匹配数量和类型的PK:

    // Create an array for the key values to find.
    object[]findTheseVals = new object[3];
    
    // Set the values of the keys to find.
    findTheseVals[0] = "John";
    findTheseVals[1] = "Smith";
    findTheseVals[2] = "5 Main St.";
    
    DataRow foundRow = table.Rows.Find(findTheseVals);
    

    您可以将值设置为null以查找您的行 .

相关问题