操纵数据集表 [英] Manipulating dataset tables

查看:81
本文介绍了操纵数据集表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,因为我得到两个基于一个表值的表我必须修改另一个表我怎样才能实现这个?



< pre lang =c#> _ dsexportData.Tables.Add(_dtdiagnosisData);





在_dsexportData我得到两个表。所以基于一个表我必须对另一个表进行更改。

解决方案

您必须通过第一个表迭代并使用第一个表中的信息来修改第二个表中的值,就像在下一个例子中一样:

 DataTable firstTable = dataSet.Tables [  FirstTableName]; 
DataTable secondTable = dataSet.Tables [ SecondTableName];
//
// 迭代第一个数据表数据
foreach (DataRow row in firstTable.Rows)
{
ModifySecondTable(rowFromFirstTable);
}
...
void ModifySecondTable(DataRow rowFromFirstTable,DataTable secondTable)
{
int fk = row [ ID]; // 从第一个表格中获取ID或PK,即第二个表格中的FK!
foreach (DataRow secondTableRow in secondTable.Rows)
{
int linkField = secondTableRow [ linkField]; // 在这里你应该填写FK的真实姓名!
if (linkField == fk)
{
// 待办事项:
// 将使用rowFromFirstTable数据的代码更新为来自secondTableRow的数据!
}
}
}


I have a dataset and in that i am getting two tables based on one table values i have to modify another table how can i achieve this??

_dsexportData.Tables.Add(_dtdiagnosisData);



In _dsexportData i am getting two tables.So based one table i have to make changes for another table.

解决方案

You have to iterate trough your first tables and use the information from first table to modify the values from the second table, like in the next example:

DataTable firstTable = dataSet.Tables["FirstTableName"];
DataTable secondTable = dataSet.Tables["SecondTableName"];
//
//Iterate trough first data table data
foreach(DataRow row in firstTable.Rows)
{
    ModifySecondTable(rowFromFirstTable);     
}
...
void ModifySecondTable(DataRow rowFromFirstTable, DataTable secondTable)
{
  int fk = row["ID"]; // Access the ID, or the PK from first table that is FK in the 2nd one!
  foreach(DataRow secondTableRow in secondTable.Rows)
  {
      int linkField = secondTableRow["linkField"]; //Here you should put the real name of the FK!
      if(linkField == fk)
      {
          // TO DO:
          //Put your code that use the data from rowFromFirstTable to update the data from secondTableRow!
      }   
  }
}


这篇关于操纵数据集表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆