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张表的记录
///
/// 合并N张表的记录 --列相加
///
/// ds里的表集合
///
public DataSet MergeDataTable(DataSet ds)
{
System.Collections.Generic.List
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;
}
///
/// 合并N张表的记录 --列相加
///
///表的集合
///
public DataTable MergeDataTable(System.Collections.Generic.List
{
//
//定义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;
}
///
/// 合并多个sql语句求出的表 --列相加
///
///
///
public DataTable GetMergeTable(System.Collections.Generic.List
{
System.Collections.Generic.List
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
return GetMergeTableByFirstColName(ds, dvalue,"","");
}
public DataSet GetMergeTableByFirstColName(DataSet ds, Dictionary
{
System.Collections.Generic.List
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
{
Dictionary
return GetMergeTableByFirstColName(tableList, dvalue,"","");
}
///
/// 根据每张表的第一列值进行合并(即第一列值相同的合并成一行)
///
/// 合并表集合(表的第一例的值要求是唯一的)
///
public DataTable GetMergeTableByFirstColName(System.Collections.Generic.List
{
//Dictionary
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
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;
}
///
/// 根据每张表的第一列值进行合并(即第一列值相同的合并成一行)
///
/// 合并表集合(表的第一例的值可以不是唯一的)
///
public DataTable GetMergeTableByFirstColName(System.Collections.Generic.List
{
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
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;
}
///
/// 根据每张表的第一列值进行合并(即第一列值相同的合并成一行)
///
/// sql语句集合
///
public DataTable GetMergeTableByFirstColName(System.Collections.Generic.List
{
System.Collections.Generic.List
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
/
/ 执行SQL语句
/
//public DataSet Query(string strSQL)
//{
// return DBUtility.DbHelperSQL.Query(strSQL);
//}
/
/ 执行一条计算查询结果语句,返回查询结果(object)。
/
//public object GetSingle(string strSQL)
//{
// return DBUtility.DbHelperSQL.GetSingle(strSQL);
//}
}
}