/// <summary>
/// 映射到数据库的临时表生成
/// </summary>
/// <param name="siList"></param>
/// <param name="type">添加Type列,1入库,2出库</param>
/// <returns></returns>
private bool getTempDataTable(List<StoreInfo> siList, int type)
{
try
{
using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(conn))
{
StoreInfo sInf = new StoreInfo();
DataTable dt = new DataTable();
//指定目标数据库的表名
bcp.DestinationTableName = "Yt_Store_StockInTemp";
//建立数据源表字段和目标表中的列(实体类中各字段名)之间的映射
System.Type t = sInf.GetType();
System.Reflection.MemberInfo[] memberInfot = t.GetMembers();
foreach (MemberInfo var in memberInfot)
{
if (var.MemberType == MemberTypes.Property)
{
string str = var.Name;
if (str.Equals("ExportCount") || str.Equals("RowCount"))
{
continue;
}
bcp.ColumnMappings.Add(str, str);
dt.Columns.Add(str);
}
}
bcp.ColumnMappings.Add("Type", "Type");
//添加Type列,1入库,2出库
dt.Columns.Add("Type");
//把库存信息表中siList的各个实体类数据转化成DataTable
foreach (StoreInfo item in siList)
{
dt = getProperties(item, dt, type);
}
//写入数据库表 dt 是数据源DataTable
bcp.WriteToServer(dt);
//关闭SqlBulkCopy实例
bcp.Close();
return true;
}
}
catch (Exception ex)
{
ErrorLog.WriteLog("入库或出库数据导入到临时表异常", "异常消息:" + ex.Message);
return false;
}
}
/// <summary>
/// 把实体类数据的字段名和值填充到DataTable里
/// </summary>
/// <typeparam name="T">实体类泛型</typeparam>
/// <param name="t">实体类</param>
/// <param name="dt">映射到数据库的临时表生成</param>
/// <param name="type">临时表type列。1入库,2出库</param>
/// <returns></returns>
private DataTable getProperties<T>(T t, DataTable dt, int type)
{
try
{
string tStr = string.Empty;
if (t == null)
{
return dt;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
return dt;
}
DataRow dr = dt.NewRow();
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
object value = item.GetValue(t, null);
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
if (name.Equals("ExportCount") || name.Equals("RowCount"))
{
continue;
}
dr[name] = value;
}
}
dr["Type"] = type;
dt.Rows.Add(dr);
return dt;
}
catch (Exception ex)
{
ErrorLog.WriteLog("把库存信息表中的实体类数据转化成DataTable时异常", "异常消息:" + ex.Message);
return new DataTable();
}
}
/// 映射到数据库的临时表生成
/// </summary>
/// <param name="siList"></param>
/// <param name="type">添加Type列,1入库,2出库</param>
/// <returns></returns>
private bool getTempDataTable(List<StoreInfo> siList, int type)
{
try
{
using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(conn))
{
StoreInfo sInf = new StoreInfo();
DataTable dt = new DataTable();
//指定目标数据库的表名
bcp.DestinationTableName = "Yt_Store_StockInTemp";
//建立数据源表字段和目标表中的列(实体类中各字段名)之间的映射
System.Type t = sInf.GetType();
System.Reflection.MemberInfo[] memberInfot = t.GetMembers();
foreach (MemberInfo var in memberInfot)
{
if (var.MemberType == MemberTypes.Property)
{
string str = var.Name;
if (str.Equals("ExportCount") || str.Equals("RowCount"))
{
continue;
}
bcp.ColumnMappings.Add(str, str);
dt.Columns.Add(str);
}
}
bcp.ColumnMappings.Add("Type", "Type");
//添加Type列,1入库,2出库
dt.Columns.Add("Type");
//把库存信息表中siList的各个实体类数据转化成DataTable
foreach (StoreInfo item in siList)
{
dt = getProperties(item, dt, type);
}
//写入数据库表 dt 是数据源DataTable
bcp.WriteToServer(dt);
//关闭SqlBulkCopy实例
bcp.Close();
return true;
}
}
catch (Exception ex)
{
ErrorLog.WriteLog("入库或出库数据导入到临时表异常", "异常消息:" + ex.Message);
return false;
}
}
/// <summary>
/// 把实体类数据的字段名和值填充到DataTable里
/// </summary>
/// <typeparam name="T">实体类泛型</typeparam>
/// <param name="t">实体类</param>
/// <param name="dt">映射到数据库的临时表生成</param>
/// <param name="type">临时表type列。1入库,2出库</param>
/// <returns></returns>
private DataTable getProperties<T>(T t, DataTable dt, int type)
{
try
{
string tStr = string.Empty;
if (t == null)
{
return dt;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
return dt;
}
DataRow dr = dt.NewRow();
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
object value = item.GetValue(t, null);
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
if (name.Equals("ExportCount") || name.Equals("RowCount"))
{
continue;
}
dr[name] = value;
}
}
dr["Type"] = type;
dt.Rows.Add(dr);
return dt;
}
catch (Exception ex)
{
ErrorLog.WriteLog("把库存信息表中的实体类数据转化成DataTable时异常", "异常消息:" + ex.Message);
return new DataTable();
}
}