741 lines
27 KiB
C#
741 lines
27 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Text;
|
||
using System.Web.Script.Serialization;
|
||
using System.Xml;
|
||
using System.Xml.Serialization;
|
||
|
||
namespace QRWL.Web.Common
|
||
{
|
||
/// <summary>
|
||
/// DataTable相关方法
|
||
/// </summary>
|
||
public class DataTableHelper
|
||
{
|
||
#region 克隆数据行
|
||
/// <summary>
|
||
/// 克隆数据行
|
||
/// </summary>
|
||
/// <param name="sourceRow">被复制的行数据</param>
|
||
/// <param name="newRow">新的行数据</param>
|
||
/// <param name="fieldNames">要复制的字段</param>
|
||
/// <returns></returns>
|
||
private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
|
||
{
|
||
foreach (string field in fieldNames)
|
||
{
|
||
newRow[field] = sourceRow[field];
|
||
}
|
||
return newRow;
|
||
}
|
||
#endregion
|
||
|
||
#region DataRow转HashTable
|
||
/// <summary>
|
||
/// DataRow转HashTable
|
||
/// </summary>
|
||
/// <param name="dr">DataRow数据源</param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
#endregion
|
||
|
||
#region DataTable转ArrayList
|
||
/// <summary>
|
||
/// DataTable转ArrayList
|
||
/// </summary>
|
||
/// <param name="dt">DataTable数据源</param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
#endregion
|
||
|
||
#region DataTable转Hashtable
|
||
/// <summary>
|
||
/// DataTable转Hashtable
|
||
/// </summary>
|
||
/// <param name="dt">DataTable数据源</param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
#endregion
|
||
|
||
#region 将DataTable指定字段内容转换成Hashtable
|
||
/// <summary>
|
||
/// 将DataTable指定字段内容转换成Hashtable
|
||
/// 存储表中对应字段的内容到Hashtable
|
||
/// </summary>
|
||
/// <param name="dt">DataTable数据源</param>
|
||
/// <param name="keyField">字段名称</param>
|
||
/// <param name="valFiled">字段键值</param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
#endregion
|
||
|
||
#region 数据行对象转化成泛型
|
||
/// <summary>
|
||
/// 数据行对象转化成泛型
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="dt">DataTable数据源</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;
|
||
}
|
||
#endregion
|
||
|
||
#region DataTable转化成xml
|
||
/// <summary>
|
||
/// DataTable 转化成xml
|
||
/// </summary>
|
||
/// <param name="dt">DataTable数据源</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;
|
||
}
|
||
#endregion
|
||
|
||
#region 比较对象是否一致
|
||
/// <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;
|
||
}
|
||
#endregion
|
||
|
||
#region 根据条件获取新DataTable
|
||
/// <summary>
|
||
/// 根据条件获取新DataTable
|
||
/// </summary>
|
||
/// <param name="dt">被复制的DataTable</param>
|
||
/// <param name="condition">查询条件</param>
|
||
/// <returns></returns>
|
||
public static DataTable GetNewDataTable(DataTable dt, string condition)
|
||
{
|
||
if (IsExistRows(dt))
|
||
{
|
||
if (condition.Trim() == "")
|
||
{
|
||
return dt;
|
||
}
|
||
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;
|
||
}
|
||
#endregion
|
||
|
||
#region DataTable分页
|
||
/// <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;
|
||
}
|
||
#endregion
|
||
|
||
#region 判断是否有数据行
|
||
/// <summary>
|
||
/// 判断是否有数据行
|
||
/// </summary>
|
||
/// <param name="dt">DataTable数据源</param>
|
||
/// <returns></returns>
|
||
public static bool IsExistRows(DataTable dt)
|
||
{
|
||
return ((dt != null) && (dt.Rows.Count > 0));
|
||
}
|
||
#endregion
|
||
|
||
#region 获取去重后的数据源
|
||
/// <summary>
|
||
/// 获取去重后的数据源
|
||
/// </summary>
|
||
/// <param name="SourceTable">DataTable数据源</param>
|
||
/// <param name="FieldNames">查重字段</param>
|
||
/// <returns></returns>
|
||
public static DataTable SelectDistinct(DataTable SourceTable, string[] FieldNames)
|
||
{
|
||
return SelectDistinct(SourceTable, FieldNames, false);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取去重后的数据源
|
||
/// </summary>
|
||
/// <param name="SourceTable">DataTable数据源</param>
|
||
/// <param name="FieldNames">查重字段</param>
|
||
/// <param name="TypeofString">返回字段类型是否都是字符串</param>
|
||
/// <returns></returns>
|
||
public static DataTable SelectDistinct(DataTable SourceTable, string[] FieldNames, bool TypeofString)
|
||
{
|
||
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, TypeofString ? typeof(string) : 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, TypeofString);
|
||
}
|
||
}
|
||
return newTable;
|
||
}
|
||
|
||
#region 获取指定字段的内容
|
||
/// <summary>
|
||
/// 获取指定字段的内容
|
||
/// </summary>
|
||
/// <param name="lastValues">返回的数据集</param>
|
||
/// <param name="sourceRow">DataRow数据源</param>
|
||
/// <param name="fieldNames">指定的字段</param>
|
||
/// <param name="TypeofString">返回字段类型是否都是字符串</param>
|
||
private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames, bool TypeofString = false)
|
||
{
|
||
for (int i = 0; i < fieldNames.Length; i++)
|
||
{
|
||
lastValues[i] = TypeofString ? sourceRow[fieldNames[i]].ToString() : sourceRow[fieldNames[i]];
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#endregion
|
||
|
||
#region 将DataTable进行正序排序
|
||
/// <summary>
|
||
/// 将DataTable进行正序排序
|
||
/// </summary>
|
||
/// <param name="dt">DataTable数据源</param>
|
||
/// <param name="sorts">排序字段,数组类型</param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
#endregion
|
||
|
||
#region 根据枚举转化对应的DataTable
|
||
/// <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);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 转换DataTable字段类型
|
||
/// <summary>
|
||
/// 转换DataTable字段类型
|
||
/// </summary>
|
||
/// <param name="table">DataTable数据源</param>
|
||
/// <returns></returns>
|
||
public static DataTable ConvertOraclTableToDoNetTable(DataTable table)
|
||
{
|
||
DataTable dt = new DataTable();
|
||
foreach (DataColumn dc in table.Columns)
|
||
{
|
||
DataColumn column = new DataColumn();
|
||
column.DataType = GetDataType(dc.DataType);
|
||
column.ColumnName = dc.ColumnName;
|
||
column.Caption = dc.Caption;
|
||
dt.Columns.Add(column);
|
||
}
|
||
dt.TableName = table.TableName;
|
||
dt.Merge(table, false, MissingSchemaAction.Ignore);
|
||
return dt;
|
||
}
|
||
#endregion
|
||
|
||
#region 解析字段类型
|
||
/// <summary>
|
||
/// 解析字段类型
|
||
/// </summary>
|
||
/// <param name="dataType">字段类型</param>
|
||
/// <returns></returns>
|
||
public static Type GetDataType(Type dataType)
|
||
{
|
||
switch (dataType.ToString())
|
||
{
|
||
case "System.Double":
|
||
return System.Type.GetType("System.Decimal");
|
||
case "System.Int32":
|
||
return System.Type.GetType("System.Decimal");
|
||
case "System.Int16":
|
||
return System.Type.GetType("System.Decimal");
|
||
case "System.Int64":
|
||
return System.Type.GetType("System.Decimal");
|
||
default:
|
||
return dataType;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region DataRow转DataTable
|
||
/// <summary>
|
||
/// DataRow转DataTable
|
||
/// </summary>
|
||
/// <param name="dt">数据来源DataTable</param>
|
||
/// <param name="strWhere">过滤成DataRow的条件</param>
|
||
/// <returns></returns>
|
||
public static DataTable DataRowToDataTable(DataTable dt, string strWhere)
|
||
{
|
||
DataTable dtNew = dt.Clone(); //复制数据源的表结构
|
||
DataRow[] dr = dt.Select(strWhere); //strWhere条件筛选出需要的数据!
|
||
for (int i = 0; i < dr.Length; i++)
|
||
{
|
||
dtNew.Rows.Add(dr[i].ItemArray); // 将DataRow添加到DataTable中
|
||
}
|
||
return dtNew;
|
||
}
|
||
#endregion
|
||
|
||
#region 判断DataTale中判断某个字段中包含某个数据
|
||
/// <summary>
|
||
/// 判断DataTale中判断某个字段中包含某个数据
|
||
/// </summary>
|
||
/// <param name="dt">数据来源DataTable</param>
|
||
/// <param name="columnName">需要比较的列</param>
|
||
/// <param name="fieldData">需要比较的具体数据</param>
|
||
/// <returns></returns>
|
||
public static Boolean IsColumnIncludeData(DataTable dt, String columnName, string fieldData)
|
||
{
|
||
if (dt == null)
|
||
{
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
DataRow[] dataRows = dt.Select(columnName + "='" + fieldData + "'");
|
||
if (dataRows.Length.Equals(1))
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 获取指定页码Datatable数据
|
||
/// <summary>
|
||
/// 获取指定页码Datatable数据
|
||
/// </summary>
|
||
/// <param name="dtOrigin">源数据</param>
|
||
/// <param name="pageSize">每页返回的数量</param>
|
||
/// <param name="pageIndex">返回第几页数据</param>
|
||
/// <returns>Datatable数据格式</returns>
|
||
public static DataTable GetDataTableWithPageSize(DataTable dtOrigin, int pageSize, int pageIndex)
|
||
{
|
||
if (pageIndex == 0)
|
||
{
|
||
pageIndex = 1;
|
||
}
|
||
if (pageSize == 0)
|
||
{
|
||
pageSize = 10;
|
||
}
|
||
//克隆数据源
|
||
DataTable dtClone = dtOrigin.Clone();
|
||
dtClone.Columns.Add("RN", typeof(int));
|
||
//开始插入数据源
|
||
for (int RowCount = 0; RowCount < pageSize; RowCount++)
|
||
{
|
||
//获取当前数据源行数
|
||
int RN = pageSize * (pageIndex - 1) + RowCount;
|
||
//如果行数大于数据源实际数量,则直接跳过
|
||
if (RN >= dtOrigin.Rows.Count)
|
||
{
|
||
break;
|
||
}
|
||
//获取当前数据源内容
|
||
DataRow EndAccountDataRow = dtOrigin.Rows[RN];
|
||
//创建一个新 System.Data.DataRow 具有与表相同的架构
|
||
DataRow _DataRow = dtClone.NewRow();
|
||
//开始插入数据
|
||
for (int ColumnsCount = 0; ColumnsCount < dtOrigin.Columns.Count; ColumnsCount++)
|
||
{
|
||
_DataRow[ColumnsCount] = EndAccountDataRow[ColumnsCount];
|
||
}
|
||
_DataRow["RN"] = RN + 1;
|
||
//将数据加入到克隆的DataTable中
|
||
dtClone.Rows.Add(_DataRow);
|
||
}
|
||
|
||
return dtClone;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 【Json字符串】转换为DataTable数据集合
|
||
/// <summary>
|
||
/// 【Json字符串】转换为DataTable数据集合
|
||
/// </summary>
|
||
/// <param name="json"></param>
|
||
/// <returns></returns>
|
||
public static DataTable ToDataTable(string json)
|
||
{
|
||
DataTable dataTable = new DataTable(); //实例化
|
||
DataTable result;
|
||
try
|
||
{
|
||
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
|
||
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
|
||
if (!string.IsNullOrEmpty(json))
|
||
{
|
||
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
|
||
if (arrayList.Count > 0)
|
||
{
|
||
foreach (Dictionary<string, object> dictionary in arrayList)
|
||
{
|
||
if (dictionary.Keys.Count == 0)
|
||
{
|
||
result = dataTable;
|
||
return result;
|
||
}
|
||
//Columns
|
||
if (dataTable.Columns.Count == 0)
|
||
{
|
||
foreach (string current in dictionary.Keys)
|
||
{
|
||
if (IsType(dictionary[current].GetType(), "System.Nullable`1[System.Int16]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Int32]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Int64]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Double]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Decimal]") ||
|
||
dictionary[current].GetType() == typeof(System.Int16) ||
|
||
dictionary[current].GetType() == typeof(System.Int32) ||
|
||
dictionary[current].GetType() == typeof(System.Int64) ||
|
||
dictionary[current].GetType() == typeof(System.Double) ||
|
||
dictionary[current].GetType() == typeof(System.Decimal))
|
||
{
|
||
dataTable.Columns.Add(current, typeof(System.Decimal));
|
||
}
|
||
else
|
||
{
|
||
dataTable.Columns.Add(current, dictionary[current].GetType());
|
||
}
|
||
}
|
||
}
|
||
//Rows
|
||
DataRow dataRow = dataTable.NewRow();
|
||
foreach (string current in dictionary.Keys)
|
||
{
|
||
try
|
||
{
|
||
dataRow[current] = dictionary[current];
|
||
}
|
||
catch { }
|
||
}
|
||
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex);
|
||
}
|
||
result = dataTable;
|
||
return result;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 【object】转换为DataTable数据集合
|
||
/// <summary>
|
||
/// object转换为DataTable数据集合
|
||
/// </summary>
|
||
/// <param name="obj">数据对象</param>
|
||
/// <returns></returns>
|
||
public static DataTable ObjectToTable(object obj)
|
||
{
|
||
Type t;
|
||
//获取对象数据类型
|
||
if (obj.GetType().IsGenericType)
|
||
{
|
||
//泛型
|
||
t = obj.GetType().GetGenericTypeDefinition();
|
||
}
|
||
else
|
||
{
|
||
t = obj.GetType();
|
||
}
|
||
if (t == typeof(List<>) || t == typeof(IEnumerable<>))
|
||
{
|
||
//是列表或者枚举
|
||
DataTable dt = new DataTable();
|
||
IEnumerable<object> lstenum = obj as IEnumerable<object>;
|
||
if (lstenum.Count() > 0)
|
||
{
|
||
var ob1 = lstenum.GetEnumerator();
|
||
ob1.MoveNext();
|
||
//遍历泛型列表字段,生成DataTable的Column
|
||
foreach (var item in ob1.Current.GetType().GetProperties())
|
||
{
|
||
dt.Columns.Add(new DataColumn() { ColumnName = item.Name });
|
||
}
|
||
//绑定数据
|
||
foreach (var item in lstenum)
|
||
{
|
||
DataRow row = dt.NewRow();
|
||
foreach (var sub in item.GetType().GetProperties())
|
||
{
|
||
row[sub.Name] = sub.GetValue(item, null);
|
||
}
|
||
dt.Rows.Add(row);
|
||
}
|
||
return dt;
|
||
}
|
||
}
|
||
else if (t == typeof(DataTable))
|
||
{
|
||
return (DataTable)obj;
|
||
}
|
||
else
|
||
{
|
||
DataTable dt = new DataTable();
|
||
//遍历object的字段,生成DataTable的Column
|
||
foreach (var item in obj.GetType().GetProperties())
|
||
{
|
||
dt.Columns.Add(new DataColumn() { ColumnName = item.Name });
|
||
}
|
||
//绑定数据
|
||
DataRow row = dt.NewRow();
|
||
foreach (var item in obj.GetType().GetProperties())
|
||
{
|
||
row[item.Name] = item.GetValue(obj, null);
|
||
}
|
||
dt.Rows.Add(row);
|
||
return dt;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 获取两个DataTable的字段集合
|
||
public static List<string> GetDataTableColumns(DataTable dtFTable, DataTable dtSTable)
|
||
{
|
||
List<string> TableColumnList = new List<string>();
|
||
//遍历第一个DataTable,加载他的字段
|
||
foreach(DataColumn dataColumn in dtFTable.Columns)
|
||
{
|
||
if (!TableColumnList.Contains(dataColumn.ColumnName))
|
||
{
|
||
TableColumnList.Add(dataColumn.ColumnName);
|
||
}
|
||
}
|
||
//遍历第二个DataTable,加载他的字段
|
||
foreach(DataColumn dataColumn in dtSTable.Columns)
|
||
{
|
||
if (!TableColumnList.Contains(dataColumn.ColumnName))
|
||
{
|
||
TableColumnList.Add(dataColumn.ColumnName);
|
||
}
|
||
}
|
||
|
||
return TableColumnList;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 类型匹配
|
||
/// <summary>
|
||
/// 类型匹配
|
||
/// </summary>
|
||
/// <param name="type">类型</param>
|
||
/// <param name="typeName">类型名称</param>
|
||
/// <returns></returns>
|
||
public static bool IsType(Type type, string typeName)
|
||
{
|
||
if (type.ToString() == typeName)
|
||
return true;
|
||
if (type.ToString() == "System.Object")
|
||
return false;
|
||
return IsType(type.BaseType, typeName);
|
||
}
|
||
#endregion
|
||
}
|
||
}
|