192 lines
6.7 KiB
C#
192 lines
6.7 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.Threading.Tasks;
|
||
|
||
namespace PosDataTest
|
||
{
|
||
class DataTableHelper
|
||
{
|
||
#region DataTable相关
|
||
/// <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>
|
||
/// 分解数据表
|
||
/// </summary>
|
||
/// <param name="originalTab">需要分解的表</param>
|
||
/// <param name="rowsNum">每个表包含的数据量</param>
|
||
/// <returns></returns>
|
||
public DataSet SplitDataTable(DataTable originalTab, int rowsNum)
|
||
{
|
||
//获取所需创建的表数量
|
||
int tableNum = (int)Math.Ceiling((decimal)originalTab.Rows.Count / rowsNum);
|
||
//获取数据余数
|
||
int remainder = originalTab.Rows.Count % rowsNum;
|
||
|
||
DataSet ds = new DataSet();
|
||
|
||
//如果只需要创建1个表,直接将原始表存入DataSet
|
||
//if (originalTab.Rows.Count <= rowsNum)
|
||
//{
|
||
// ds.Tables.Add(originalTab);
|
||
//}
|
||
//else
|
||
//{
|
||
DataTable[] tableSlice = new DataTable[tableNum];
|
||
|
||
//Save orginal columns into new table.
|
||
for (int c = 0; c < tableNum; c++)
|
||
{
|
||
tableSlice[c] = originalTab.Clone();
|
||
}
|
||
//Import Rows
|
||
for (int i = 0; i < tableNum; i++)
|
||
{
|
||
// if the current table is not the last one
|
||
//if (i != tableNum - 1)
|
||
//{
|
||
for (int j = i * rowsNum; j < ((i + 1) * rowsNum); j++)
|
||
{
|
||
if (j >= originalTab.Rows.Count)
|
||
{
|
||
break;
|
||
}
|
||
tableSlice[i].ImportRow(originalTab.Rows[j]);
|
||
}
|
||
//}
|
||
//else
|
||
//{
|
||
// for (int k = i * rowsNum; k < ((i + 1) * rowsNum + remainder); k++)
|
||
// {
|
||
// tableSlice[i].ImportRow(originalTab.Rows[k]);
|
||
// }
|
||
//}
|
||
}
|
||
|
||
//add all tables into a dataset
|
||
foreach (DataTable dt in tableSlice)
|
||
{
|
||
ds.Tables.Add(dt);
|
||
}
|
||
//}
|
||
return ds;
|
||
}
|
||
/// <summary>
|
||
/// 获取列名
|
||
/// </summary>
|
||
/// <param name="Table"></param>
|
||
/// <returns></returns>
|
||
internal string SqlColumns(DataTable Table)
|
||
{
|
||
string _Columnsstr = "";
|
||
foreach (DataColumn col in Table.Columns)
|
||
{
|
||
_Columnsstr += (_Columnsstr == "" ? col.ColumnName : "," + col.ColumnName);
|
||
}
|
||
return _Columnsstr;
|
||
}
|
||
/// <summary>
|
||
/// 获取行值
|
||
/// </summary>
|
||
/// <param name="Table"></param>
|
||
/// <param name="Row"></param>
|
||
/// <returns></returns>
|
||
internal string SqlValues(DataTable Table, DataRow Row)
|
||
{
|
||
string _valuesstr = "";
|
||
for (int i = 0; i < Table.Columns.Count; i++)
|
||
{
|
||
if (Table.Columns[i].DataType.ToString() == "System.Decimal")
|
||
{
|
||
_valuesstr += _valuesstr == "" ? (Row[i].ToString() == "" ? "NULL" : Row[i].ToString()) : "," + (Row[i].ToString() == "" ? "NULL" : Row[i].ToString());
|
||
}
|
||
else if (Table.Columns[i].DataType.ToString() == "System.DateTime")
|
||
{
|
||
_valuesstr += _valuesstr == "" ? (Row[i].ToString() == "" ? "NULL" : "TO_DATE('" + Row[i].ToString() +
|
||
"','YYYY/MM/DD HH24:MI:SS')") : "," + (Row[i].ToString() == "" ? "NULL" : "TO_DATE('" + Row[i].ToString() + "','YYYY/MM/DD HH24:MI:SS')");
|
||
}
|
||
else
|
||
{
|
||
_valuesstr += _valuesstr == "" ? (Row[i].ToString() == "" ? "NULL" : "'" + Row[i].ToString()) + "'" : "," + (Row[i].ToString() == "" ? "NULL" : "'" + Row[i].ToString() + "'");
|
||
}
|
||
}
|
||
return _valuesstr;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 转换DataTable字段类型
|
||
/// </summary>
|
||
/// <param name="table"></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;
|
||
}
|
||
/// <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
|
||
}
|
||
}
|