1

您好我正在尝试组合以下两个数据表,代码没有出错,但它没有在输出中组合它们。我在这条线上遇到了麻烦:

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;
    }
  }
4

1 回答 1

0

你也可以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

于 2012-10-04T15:04:41.423 回答