使用数据关系合并两个数据表时出现问题



嗨,我正在尝试组合以下两个数据表,代码没有错误,但是它没有在输出中组合它们。我在使用这一行时遇到问题:

if (pdc.ColumnName != "ORDER_NUMBER") 
    dac[pdc.ColumnName] = dsCheck.Tables[0].Rows.Find(dac[pdc.ColumnName]);

法典:

    public string GetData()
    {
        System.Data.DataSet dsCheck = new System.Data.DataSet();

        dsCheck.Tables.Add(this.GetOrder());
        //dsCheck.Tables.Add(this.GetSAP_Data());
        dsCheck.Tables.Add(GetFIS_Data());
        //return ds;
        //Create relationship
        // Create the array of Parent and Child columns.
      DataColumn ParentCol ;
      DataColumn ChildCol ;
      ParentCol = new DataColumn() ;
      ChildCol = new DataColumn() ;
      // Set the name of the parent column to hold the relationship.
      ParentCol.DataType = System.Type.GetType("System.String");
      ParentCol = dsCheck.Tables[0].Columns["ORDER_NUMBER"];

      // Set the name of the child column to hold the relationship.
      ChildCol.DataType = System.Type.GetType("System.String");
      ChildCol = dsCheck.Tables[1].Columns["KOMNR"];
      // Create the relationship
      DataRelation Rel = new DataRelation("DataLink", ParentCol, ChildCol,false) ;
      // Add the newly created relationship to the dataset.
      dsCheck.Relations.Add(Rel) ;

      //add parent columns to child DataTable
      foreach (DataColumn dac in dsCheck.Tables[0].Columns)
      {
          if (!dsCheck.Tables[1].Columns.Contains(dac.ColumnName))
              dsCheck.Tables[1].Columns.Add(dac.ColumnName);
      }

     //add parent data to child DataTable
      foreach (DataRow dac in dsCheck.Tables[1].Rows)
      {
          foreach (DataColumn pdc in dsCheck.Tables[0].Columns)
          {
              if (pdc.ColumnName != "ORDER_NUMBER")
                  dac[pdc.ColumnName] = dsCheck.Tables[0].Rows.Find(dac[pdc.ColumnName]);
          }
      }
      StringWriter sw = new StringWriter();
      dsCheck.WriteXml(sw);
      string result = sw.ToString();
      return result;
    }
  }

您也可以adjust boolean to true以创建约束

DataRelation Rel = new DataRelation("DataLink", 
dsCheck.Tables[0].Columns["ORDER_NUMBER"], 
dsCheck.Tables[1].Columns["KOMNR"],
true) ;
dsCheck.Relations.Add(Rel) ;

注意:您可以尝试不通过临时列。

链接 : http://msdn.microsoft.com/fr-fr/library/9ae5a582(v=vs.80).aspx

最新更新