合并多个DataTable统计数据
using System;
 using System.Data;
 using System.Text;
 using System.Collections.Generic;
 using System.Data.SqlClient;
 using BLCard.IDAL;
 using DBUtility;
namespace BLCard.SQLServerDAL
 {
 ? ? public partial class MergeTable
 ? ? {
? ? ? ? #region 合并N張表的記錄
? ? ? ? /// <summary>
 ? ? ? ? /// 合并N張表的記錄 ? --列相加
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="ds">ds里的表集合</param>
 ? ? ? ? /// <returns></returns>
 ? ? ? ? public DataSet MergeDataTable(DataSet ds)
 ? ? ? ? {
 ? ? ? ? ? ? System.Collections.Generic.List<DataTable> tableList = new List<DataTable>();
 ? ? ? ? ? ? if (ds == null || ds.Tables.Count < 1) { return null; }
 ? ? ? ? ? ? if (ds.Tables.Count == 1) { return ds; }
 ? ? ? ? ? ? for (int i = 0; i < ds.Tables.Count; i++)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? tableList.Add(ds.Tables[i]);
 ? ? ? ? ? ? }
 ? ? ? ? ? ? DataTable _table = MergeDataTable(tableList);
 ? ? ? ? ? ? DataSet rtds = new DataSet();
 ? ? ? ? ? ? rtds.Tables.Add(_table);
 ? ? ? ? ? ? return rtds;
 ? ? ? ? }
 ? ? ? ? /// <summary>
 ? ? ? ? /// 合并N張表的記錄 ? --列相加
 ? ? ? ? /// </summary>
 ? ? ? ? ///<param name="tableList">表的集合</param>
 ? ? ? ? /// <returns></returns>
 ? ? ? ? public DataTable MergeDataTable(System.Collections.Generic.List<DataTable> tableList)
 ? ? ? ? {
 ? ? ? ? ? ? //
 ? ? ? ? ? ? //定義dt的行數
 ? ? ? ? ? ? int dtRowCount = 0;
 ? ? ? ? ? ? DataTable dt = new DataTable();
 ? ? ? ? ? ? for (int t = 0; t < tableList.Count; t++)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? if (dtRowCount < tableList[t].Rows.Count)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? dtRowCount = tableList[t].Rows.Count;
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? for (int i = 0; i < tableList[t].Columns.Count; i++)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? dt.Columns.Add(tableList[t].Columns[i].ColumnName.ToString().Trim());
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? }
 ? ? ? ? ? ? for (int i = 0; i < dtRowCount; i++)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? DataRow row = dt.NewRow();
 ? ? ? ? ? ? ? ? for (int j = 0; j < dt.Columns.Count; j++)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? int ik = -1;
 ? ? ? ? ? ? ? ? ? ? for (int t = 0; t < tableList.Count; t++)
 ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? for (int k = 0; k < tableList[t].Columns.Count; k++)
 ? ? ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ik++;
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ((tableList[t].Rows.Count - 1) >= i)
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? row[ik] = tableList[t].Rows[i].ItemArray[k];
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? dt.Rows.Add(row);
 ? ? ? ? ? ? }
 ? ? ? ? ? ? return dt;
 ? ? ? ? }
? ? ? ? /// <summary>
 ? ? ? ? /// 合并多個sql語句求出的表 ? --列相加
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="SqlList"></param>
 ? ? ? ? /// <returns></returns>
 ? ? ? ? public DataTable GetMergeTable(System.Collections.Generic.List<string> SqlList)
 ? ? ? ? {
 ? ? ? ? ? ? System.Collections.Generic.List<DataTable> tableList = new List<DataTable>();
 ? ? ? ? ? ? for (int i = 0; i < SqlList.Count; i++)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? DataSet ds = DBUtility.DbHelperSQL.Query(SqlList[i].Trim());
 ? ? ? ? ? ? ? ? if (ds != null && ds.Tables.Count > 0)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? tableList.Add(ds.Tables[0]);
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? }
 ? ? ? ? ? ? return MergeDataTable(tableList);
 ? ? ? ? }
 ? ? ? ? #endregion
? ? ? ? #region ?根據每張表的第一列值進行合并(即第一列值相同的合并成一行)
 ? ? ? ? public DataSet GetMergeTableByFirstColName(DataSet ds)
 ? ? ? ? {
 ? ? ? ? ? ? Dictionary<string, object> dvalue = new Dictionary<string, object>();
 ? ? ? ? ? ? return GetMergeTableByFirstColName(ds, dvalue,"","");
 ? ? ? ? }
 ? ? ? ? public DataSet GetMergeTableByFirstColName(DataSet ds, Dictionary<string, object> dvalue,string sorCol,string sorType)
 ? ? ? ? {
 ? ? ? ? ? ? System.Collections.Generic.List<DataTable> tableList = new List<DataTable>();
 ? ? ? ? ? ? if (ds == null || ds.Tables.Count < 1) { return null; }
 ? ? ? ? ? ? if (ds.Tables.Count == 1) { return ds; }
 ? ? ? ? ? ? for (int i = 0; i < ds.Tables.Count; i++)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? tableList.Add(ds.Tables[i]);
 ? ? ? ? ? ? }
 ? ? ? ? ? ? DataTable _table = GetMergeTableByFirstColName(tableList, dvalue,sorCol,sorType);
 ? ? ? ? ? ? DataSet rtds = new DataSet();
 ? ? ? ? ? ? rtds.Tables.Add(_table);
 ? ? ? ? ? ? return rtds;
 ? ? ? ? }
 ? ? ? ? public DataTable GetMergeTableByFirstColName(System.Collections.Generic.List<DataTable> tableList)
 ? ? ? ? {
 ? ? ? ? ? ? Dictionary<string, object> dvalue = new Dictionary<string, object>();
? ? ? ? ? ? return GetMergeTableByFirstColName(tableList, dvalue,"","");
 ? ? ? ? }
 ? ? ? ? ? ? /// <summary>
 ? ? ? ? ? ? /// 根據每張表的第一列值進行合并(即第一列值相同的合并成一行)
 ? ? ? ? ? ? /// </summary>
 ? ? ? ? ? ? /// <param name="tableList">合并表集合(表的第一例的值要求是唯一的)</param>
 ? ? ? ? ? ? /// <returns></returns>
 ? ? ? ? ?public DataTable GetMergeTableByFirstColName(System.Collections.Generic.List<DataTable> tableList,Dictionary<string, object> dvalue, string sorCol, string sorType)
 ? ? ? ? {
 ? ? ? ? ? ? //Dictionary<string, object> dvalue = new Dictionary<string, object>();
 ? ? ? ? ? ? DataTable table = new DataTable();
 ? ? ? ? ? ? if (tableList.Count > 0)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? table = tableList[0].Copy();
 ? ? ? ? ? ? }
 ? ? ? ? ? ? for (int i = 1; i < tableList.Count; i++)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? #region
? ? ? ? ? ? ? ? for (int c = 1; c < tableList[i].Columns.Count; c++)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? string cName = tableList[i].Columns[c].ColumnName.Trim();
 ? ? ? ? ? ? ? ? ? ? //tableList[i]的列添加到table里
 ? ? ? ? ? ? ? ? ? ? table.Columns.Add(cName);
 ? ? ? ? ? ? ? ? ? ? if (dvalue.ContainsKey(cName))
 ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? table.Columns[cName].DefaultValue = dvalue[cName];
 ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? System.Collections.Generic.List<string> KeyValueList = new List<string>();
 ? ? ? ? ? ? ? ? for (int r = 0; r < table.Rows.Count; r++)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? //按table的順序查找tableList[i]是否有符合的值
 ? ? ? ? ? ? ? ? ? ? string temValue = table.Rows[r][0].ToString().Trim();
 ? ? ? ? ? ? ? ? ? ? DataRow[] rows = tableList[i].Select("" + tableList[i].Columns[0].ColumnName + "='" + temValue + "'" + (temValue.Trim() == "" ? " or ?" + tableList[i].Columns[0].ColumnName + " is null " : ""));
 ? ? ? ? ? ? ? ? ? ? if (rows.Length > 0)
 ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? for (int c = 1; c < tableList[i].Columns.Count; c++)
 ? ? ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? table.Rows[r][tableList[i].Columns[c].ColumnName.Trim()] = rows[0][c].ToString().Trim();
 ? ? ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? ? ? KeyValueList.Add(table.Rows[r][0].ToString().Trim());
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? for (int l = 0; l < tableList[i].Rows.Count; l++)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? if (!KeyValueList.Contains(tableList[i].Rows[l][0].ToString().Trim()))
 ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? DataRow row = table.NewRow();
 ? ? ? ? ? ? ? ? ? ? ? ? row[0] = tableList[i].Rows[l][0].ToString().Trim();
 ? ? ? ? ? ? ? ? ? ? ? ? for (int c = 1; c < tableList[i].Columns.Count; c++)
 ? ? ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? row[tableList[i].Columns[c].ColumnName] = tableList[i].Rows[l][c].ToString().Trim();
 ? ? ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? ? ? ? ? table.Rows.Add(row);
 ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? #endregion
 ? ? ? ? ? ? }
 ? ? ? ? ? ? if(sorType!="asc" && sorType != "desc") { sorType = "asc"; }
 ? ? ? ? ? ? if(sorCol.Trim()!="")
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? if(table.Columns.Contains(sorCol))
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? DataView dv = new DataView(table);
 ? ? ? ? ? ? ? ? ? ? dv.Sort = sorCol +" "+ sorType;
 ? ? ? ? ? ? ? ? ? ? table = dv.ToTable();
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? }
 ? ? ? ? ? ? return table;
 ? ? ? ? }
 ? ? ? ? /// <summary>
 ? ? ? ? /// 根據每張表的第一列值進行合并(即第一列值相同的合并成一行)
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="tableList">合并表集合(表的第一例的值可以不是唯一的)</param>
 ? ? ? ? /// <returns></returns>
 ? ? ? ? public DataTable GetMergeTableByFirstColName(System.Collections.Generic.List<DataTable> tableList,int iCols)
 ? ? ? ? {
 ? ? ? ? ? ? DataTable table = new DataTable();
 ? ? ? ? ? ? if (tableList.Count > 0)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? table = tableList[0].Copy();
 ? ? ? ? ? ? }
 ? ? ? ? ? ? for (int i = 1; i < tableList.Count; i++)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? #region
? ? ? ? ? ? ? ? for (int c = 1; c < tableList[i].Columns.Count; c++)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? //tableList[i]的列添加到table里
 ? ? ? ? ? ? ? ? ? ? table.Columns.Add(tableList[i].Columns[c].ColumnName.Trim());
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? System.Collections.Generic.List<string> KeyValueList = new List<string>();
 ? ? ? ? ? ? ? ? for (int r = 0; r < table.Rows.Count; r++)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? //按table的順序查找tableList[i]是否有符合的值
 ? ? ? ? ? ? ? ? ? ? string temValue = table.Rows[r][0].ToString().Trim();
 ? ? ? ? ? ? ? ? ? ? DataRow[] rows = tableList[i].Select("" + tableList[i].Columns[0].ColumnName + "='" + temValue + "'" + (temValue.Trim() == "" ? " or ?" + tableList[i].Columns[0].ColumnName + " is null " : ""));
 ? ? ? ? ? ? ? ? ? ? if (rows.Length > 0)
 ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? for (int c = 1; c < tableList[i].Columns.Count; c++)
 ? ? ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? table.Rows[r][tableList[i].Columns[c].ColumnName.Trim()] = rows[0][c].ToString().Trim();
 ? ? ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? ? ? KeyValueList.Add(table.Rows[r][0].ToString().Trim());
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? for (int l = 0; l < tableList[i].Rows.Count; l++)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? if (!KeyValueList.Contains(tableList[i].Rows[l][0].ToString().Trim()))
 ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? DataRow row = table.NewRow();
 ? ? ? ? ? ? ? ? ? ? ? ? row[0] = tableList[i].Rows[l][0].ToString().Trim();
 ? ? ? ? ? ? ? ? ? ? ? ? for (int c = 1; c < tableList[i].Columns.Count; c++)
 ? ? ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? row[tableList[i].Columns[c].ColumnName] = tableList[i].Rows[l][c].ToString().Trim();
 ? ? ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? ? ? ? ? table.Rows.Add(row);
 ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? #endregion
? ? ? ? ? ? }
 ? ? ? ? ? ? return table;
 ? ? ? ? }
 ? ? ? ? /// <summary>
 ? ? ? ? /// 根據每張表的第一列值進行合并(即第一列值相同的合并成一行)
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="SqlList">sql語句集合</param>
 ? ? ? ? /// <returns></returns>
 ? ? ? ? public DataTable GetMergeTableByFirstColName(System.Collections.Generic.List<string> SqlList)
 ? ? ? ? {
 ? ? ? ? ? ? System.Collections.Generic.List<DataTable> tableList = new List<DataTable>();
 ? ? ? ? ? ? for (int i = 0; i < SqlList.Count; i++)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? DataSet ds_Tem = DBUtility.DbHelperSQL.Query(SqlList[i]);
 ? ? ? ? ? ? ? ? if (ds_Tem != null && ds_Tem.Tables.Count > 0)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? tableList.Add(ds_Tem.Tables[0]);
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? }
 ? ? ? ? ? ? return GetMergeTableByFirstColName(tableList);
 ? ? ? ? }
 ? ? ? ? #endregion
 ? ? ? ? / <summary>
 ? ? ? ? / 執行SQL語句
 ? ? ? ? / </summary>
 ? ? ? ? //public DataSet Query(string strSQL)
 ? ? ? ? //{
 ? ? ? ? // ? ?return DBUtility.DbHelperSQL.Query(strSQL);
 ? ? ? ? //}
? ? ? ? / <summary>
 ? ? ? ? / 執行一條計算查詢結果語句,返回查詢結果(object)。
 ? ? ? ? / </summary>
 ? ? ? ? //public object GetSingle(string strSQL)
 ? ? ? ? //{
 ? ? ? ? // ? ?return DBUtility.DbHelperSQL.GetSingle(strSQL);
 ? ? ? ? //}
 ? ? }
 }
 ?
總結
以上是生活随笔為你收集整理的合并多个DataTable统计数据的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 博主日常工作中使用的shell脚本分享
- 下一篇: [kuangbin带你飞]专题四 最短路
