zoukankan      html  css  js  c++  java
  • 程序中实现两个DataTable的Left Join效果(修改了,网上第二个DataTable为空,所处的异常)

          public static DataTable Join(DataTable First, DataTable Second, DataColumn[] FJC, DataColumn[] SJC)
          {
              DataTable table = new DataTable("Join");
              using (DataSet ds = new DataSet())
              {
                  ds.Tables.AddRange(new DataTable[] { First.Copy(), Second.Copy() });
                  DataColumn[] First_columns = new DataColumn[FJC.Length];
                  for (int i = 0; i < First_columns.Length; i++)
                  {
                      First_columns[i] = ds.Tables[0].Columns[FJC[i].ColumnName];
                  }
                  DataColumn[] Second_columns = new DataColumn[SJC.Length];
                  for (int i = 0; i < Second_columns.Length; i++)
                  {
                      Second_columns[i] = ds.Tables[1].Columns[SJC[i].ColumnName];
                  }
                  DataRelation r = new DataRelation(string.Empty, First_columns, Second_columns, false);
                  ds.Relations.Add(r);
    
                  for (int i = 0; i < First.Columns.Count; i++)
                  {
                      table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);
                  }
    
                  for (int i = 0; i < Second.Columns.Count; i++)
                  {
    
                      //看看有没有重复的列,如果有在第二个DataTable的Column的列明后加_Second
                      if (!table.Columns.Contains(Second.Columns[i].ColumnName))
                          table.Columns.Add(Second.Columns[i].ColumnName, Second.Columns[i].DataType);
                      else
                          table.Columns.Add(Second.Columns[i].ColumnName + "_1", Second.Columns[i].DataType);
                  }
                  table.BeginLoadData();
    
                  int itable2Colomns = 0;
                  if (ds.Tables[1].Rows.Count > 0) {
                      itable2Colomns = ds.Tables[1].Rows[0].ItemArray.Length;
                  }
                      
                  foreach (DataRow firstrow in ds.Tables[0].Rows)
                  {
                      //得到行的数据
                      DataRow[] childrows = firstrow.GetChildRows(r);//第二个表关联的行
                      if (childrows != null && childrows.Length > 0)
                      {
                          object[] parentarray = firstrow.ItemArray;
                          foreach (DataRow secondrow in childrows)
                          {
                              object[] secondarray = secondrow.ItemArray;
                              object[] joinarray = new object[parentarray.Length + secondarray.Length];
                              Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
                              Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);
                              table.LoadDataRow(joinarray, true);
                          }
                      }
                      else//如果有外连接(Left Join)添加这部分代码
                      {
                          object[] table1array = firstrow.ItemArray;//Table1
                          object[] table2array = new object[itable2Colomns];
                          object[] joinarray = new object[table1array.Length + itable2Colomns];
                          Array.Copy(table1array, 0, joinarray, 0, table1array.Length);
                          Array.Copy(table2array, 0, joinarray, table1array.Length, itable2Colomns);
                          table.LoadDataRow(joinarray, true);
                          DataColumn[] dc = new DataColumn[2];
                          dc[0] = new DataColumn("");
                      }
                  }
                  table.EndLoadData();
              }
              return table;
          }
  • 相关阅读:
    p3159 [CQOI2012]交换棋子
    三分法
    p2805 [NOI2009]植物大战僵尸
    p2604 [ZJOI2010]网络扩容
    p1129 [ZJOI2007]矩阵游戏
    有趣与愉快-------罗辑思维整理
    张小龙的书单
    会议
    使用CCProxy代理遇到的问题
    关于看书
  • 原文地址:https://www.cnblogs.com/qizhelongdeyang/p/3523188.html
Copyright © 2011-2022 走看看