日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

合并多个DataTable统计数据

發(fā)布時(shí)間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 合并多个DataTable统计数据 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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的行數(shù)
? ? ? ? ? ? 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>
? ? ? ? /// 合并多個(gè)sql語(yǔ)句求出的表 ? --列相加
? ? ? ? /// </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 ?根據(jù)每張表的第一列值進(jìn)行合并(即第一列值相同的合并成一行)
? ? ? ? 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>
? ? ? ? ? ? /// 根據(jù)每張表的第一列值進(jìn)行合并(即第一列值相同的合并成一行)
? ? ? ? ? ? /// </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>
? ? ? ? /// 根據(jù)每張表的第一列值進(jìn)行合并(即第一列值相同的合并成一行)
? ? ? ? /// </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>
? ? ? ? /// 根據(jù)每張表的第一列值進(jìn)行合并(即第一列值相同的合并成一行)
? ? ? ? /// </summary>
? ? ? ? /// <param name="SqlList">sql語(yǔ)句集合</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>
? ? ? ? / 執(zhí)行SQL語(yǔ)句
? ? ? ? / </summary>
? ? ? ? //public DataSet Query(string strSQL)
? ? ? ? //{
? ? ? ? // ? ?return DBUtility.DbHelperSQL.Query(strSQL);
? ? ? ? //}

? ? ? ? / <summary>
? ? ? ? / 執(zhí)行一條計(jì)算查詢結(jié)果語(yǔ)句,返回查詢結(jié)果(object)。
? ? ? ? / </summary>
? ? ? ? //public object GetSingle(string strSQL)
? ? ? ? //{
? ? ? ? // ? ?return DBUtility.DbHelperSQL.GetSingle(strSQL);
? ? ? ? //}
? ? }
}
?

總結(jié)

以上是生活随笔為你收集整理的合并多个DataTable统计数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。