306 lines
10 KiB
C#
306 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Data;
|
|
using System.Xml;
|
|
using System.Xml.Serialization;
|
|
using System.Collections;
|
|
using System.Reflection;
|
|
|
|
namespace HiiShe.Manager
|
|
{
|
|
public class DataTableHelper
|
|
{
|
|
|
|
private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
|
|
{
|
|
foreach (string field in fieldNames)
|
|
{
|
|
newRow[field] = sourceRow[field];
|
|
}
|
|
return newRow;
|
|
}
|
|
|
|
public static Hashtable DataRowToHashTable(DataRow dr)
|
|
{
|
|
Hashtable htReturn = new Hashtable(dr.ItemArray.Length);
|
|
foreach (DataColumn dc in dr.Table.Columns)
|
|
{
|
|
htReturn.Add(dc.ColumnName, dr[dc.ColumnName]);
|
|
}
|
|
return htReturn;
|
|
}
|
|
|
|
public static IList<Hashtable> DataTableToArrayList(DataTable dt)
|
|
{
|
|
if (dt == null)
|
|
{
|
|
return new List<Hashtable>();
|
|
}
|
|
IList<Hashtable> datas = new List<Hashtable>();
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
Hashtable ht = DataRowToHashTable(dr);
|
|
datas.Add(ht);
|
|
}
|
|
return datas;
|
|
}
|
|
|
|
public static Hashtable DataTableToHashtable(DataTable dt)
|
|
{
|
|
Hashtable ht = new Hashtable();
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
for (int i = 0; i < dt.Columns.Count; i++)
|
|
{
|
|
string key = dt.Columns[i].ColumnName;
|
|
ht[key.ToUpper()] = dr[key];
|
|
}
|
|
}
|
|
return ht;
|
|
}
|
|
|
|
public static Hashtable DataTableToHashtableByKeyValue(DataTable dt, string keyField, string valFiled)
|
|
{
|
|
Hashtable ht = new Hashtable();
|
|
if (dt != null)
|
|
{
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
string key = dr[keyField].ToString();
|
|
ht[key] = dr[valFiled];
|
|
}
|
|
}
|
|
return ht;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据行对象转化成泛型
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="dt"></param>
|
|
/// <returns></returns>
|
|
public static IList DataTableToIList<T>(DataTable dt)
|
|
{
|
|
IList list = new List<T>();
|
|
string tempName = "";
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
T obj = Activator.CreateInstance<T>();
|
|
PropertyInfo[] propertys = obj.GetType().GetProperties();
|
|
foreach (PropertyInfo pi in propertys)
|
|
{
|
|
tempName = pi.Name;
|
|
if (dt.Columns.Contains(tempName) && pi.CanWrite)
|
|
{
|
|
object value = dr[tempName];
|
|
if (value != DBNull.Value)
|
|
{
|
|
pi.SetValue(obj, value, null);
|
|
}
|
|
}
|
|
}
|
|
list.Add(obj);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable 转化成xml
|
|
/// </summary>
|
|
/// <param name="dt"></param>
|
|
/// <returns></returns>
|
|
public static string DataTableToXML(DataTable dt)
|
|
{
|
|
if (dt != null)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
XmlWriter writer = XmlWriter.Create(sb);
|
|
new XmlSerializer(typeof(DataTable)).Serialize(writer, dt);
|
|
writer.Close();
|
|
return sb.ToString();
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 比较对象是否一致
|
|
/// </summary>
|
|
/// <param name="lastValues"></param>
|
|
/// <param name="currentRow"></param>
|
|
/// <param name="fieldNames"></param>
|
|
/// <returns></returns>
|
|
private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
|
|
{
|
|
for (int i = 0; i < fieldNames.Length; i++)
|
|
{
|
|
if (!((lastValues[i] != null) && lastValues[i].Equals(currentRow[fieldNames[i]])))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
public static DataTable GetNewDataTable(DataTable dt, string condition)
|
|
{
|
|
if (IsExistRows(dt))
|
|
{
|
|
if (condition.Trim() == "")
|
|
{
|
|
return dt;
|
|
}
|
|
DataTable newdt = new DataTable();
|
|
newdt = dt.Clone();
|
|
DataRow[] dr = dt.Select(condition);
|
|
for (int i = 0; i < dr.Length; i++)
|
|
{
|
|
newdt.ImportRow(dr[i]);
|
|
}
|
|
return newdt;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable 分页
|
|
/// </summary>
|
|
/// <param name="dt">DataTable</param>
|
|
/// <param name="PageIndex">当前页</param>
|
|
/// <param name="PageSize">页码</param>
|
|
/// <returns></returns>
|
|
public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
|
|
{
|
|
if (PageIndex == 0)
|
|
{
|
|
return dt;
|
|
}
|
|
DataTable newdt = dt.Copy();
|
|
newdt.Clear();
|
|
int rowbegin = (PageIndex - 1) * PageSize;
|
|
int rowend = PageIndex * PageSize;
|
|
if (rowbegin < dt.Rows.Count)
|
|
{
|
|
if (rowend > dt.Rows.Count)
|
|
{
|
|
rowend = dt.Rows.Count;
|
|
}
|
|
for (int i = rowbegin; i <= (rowend - 1); i++)
|
|
{
|
|
DataRow newdr = newdt.NewRow();
|
|
DataRow dr = dt.Rows[i];
|
|
foreach (DataColumn column in dt.Columns)
|
|
{
|
|
newdr[column.ColumnName] = dr[column.ColumnName];
|
|
}
|
|
newdt.Rows.Add(newdr);
|
|
}
|
|
}
|
|
return newdt;
|
|
}
|
|
|
|
public static bool IsExistRows(DataTable dt)
|
|
{
|
|
return ((dt != null) && (dt.Rows.Count > 0));
|
|
}
|
|
|
|
public static DataTable SelectDistinct(DataTable SourceTable, string[] FieldNames)
|
|
{
|
|
if ((FieldNames == null) || (FieldNames.Length == 0))
|
|
{
|
|
throw new ArgumentNullException("FieldNames");
|
|
}
|
|
object[] lastValues = new object[FieldNames.Length];
|
|
DataTable newTable = new DataTable();
|
|
foreach (string fieldName in FieldNames)
|
|
{
|
|
newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);
|
|
}
|
|
DataRow[] orderedRows = SourceTable.Select("", string.Join(",", FieldNames));
|
|
foreach (DataRow row in orderedRows)
|
|
{
|
|
if (!fieldValuesAreEqual(lastValues, row, FieldNames))
|
|
{
|
|
newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));
|
|
setLastValues(lastValues, row, FieldNames);
|
|
}
|
|
}
|
|
return newTable;
|
|
}
|
|
|
|
private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
|
|
{
|
|
for (int i = 0; i < fieldNames.Length; i++)
|
|
{
|
|
lastValues[i] = sourceRow[fieldNames[i]];
|
|
}
|
|
}
|
|
|
|
public static DataTable SortedTable(DataTable dt, params string[] sorts)
|
|
{
|
|
if (dt.Rows.Count > 0)
|
|
{
|
|
string tmp = "";
|
|
for (int i = 0; i < sorts.Length; i++)
|
|
{
|
|
tmp = tmp + sorts[i] + ",";
|
|
}
|
|
dt.DefaultView.Sort = tmp.TrimEnd(new char[] { ',' });
|
|
}
|
|
return dt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据枚举转化对应的DataTable
|
|
/// </summary>
|
|
public static void DataTableTanslate(DataTable dataTable, Dictionary<string, Dictionary<string, string>> dictionary)
|
|
{
|
|
if (dictionary == null)
|
|
{
|
|
return;
|
|
}
|
|
string str = "_" + Guid.NewGuid().ToString().Substring(0, 10).Replace("-","_");
|
|
foreach (DataRow dataRow in dataTable.Rows)
|
|
{
|
|
foreach (string current in dictionary.Keys)
|
|
{
|
|
if (dataTable.Columns.IndexOf(current) >= 0)
|
|
{
|
|
if (dataTable.Columns.IndexOf(current + str) < 0)
|
|
{
|
|
dataTable.Columns[current].ColumnName = current + str;
|
|
dataTable.Columns.Add(current, typeof(string));
|
|
}
|
|
string text = "";
|
|
if (dictionary[current].TryGetValue(dataRow[current + str].ToString(), out text))
|
|
{
|
|
dataRow[current] = text;
|
|
}
|
|
else
|
|
{
|
|
string[] array = dataRow[current + str].ToString().Split(new char[]
|
|
{
|
|
','
|
|
});
|
|
if (array.Length > 1)
|
|
{
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
if (dictionary[current].TryGetValue(array[i].ToString(), out text))
|
|
{
|
|
array[i] = text;
|
|
}
|
|
}
|
|
}
|
|
dataRow[current] = string.Join(",", array);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|