using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YX.Entity; namespace YX.DAL.Extenstions
{
public static class SqlBulkCopyHelper
{
public static void BulkInsertAll<T>(IEnumerable<T> entities)
{
entities = entities.ToArray();
var cons = new Sam_DBEntities();
string cs = cons.Database.Connection.ConnectionString;
var conn = new SqlConnection(cs);
conn.Open(); Type t = typeof(T); var bulkCopy = new SqlBulkCopy(conn)
{
DestinationTableName = t.Name
}; var properties = t.GetProperties().Where(EventTypeFilter).ToArray();
var table = new DataTable(); foreach (var property in properties)
{ Type propertyType = property.PropertyType;
if (propertyType.IsGenericType &&
propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = Nullable.GetUnderlyingType(propertyType);
} table.Columns.Add(new DataColumn(property.Name, propertyType));
} foreach (var entity in entities)
{ table.Rows.Add(properties.Select(
property => GetPropertyValue(
property.GetValue(entity, null), property.PropertyType.Name)).ToArray());
}
foreach (DataColumn dc in table.Columns)
{
bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(dc.ColumnName, dc.ColumnName));
}
bulkCopy.WriteToServer(table); conn.Close();
} private static bool EventTypeFilter(System.Reflection.PropertyInfo p)
{
var attribute = Attribute.GetCustomAttribute(p,
typeof(AssociationAttribute)) as AssociationAttribute; if (attribute == null) return true;
if (attribute.IsForeignKey == false) return true; return false;
} private static object GetPropertyValue(object o, string type)
{
if (o == null)
{
return DBNull.Value;
} return o;
}
}
}

今天使用SqlBulkCopy时,一直报错。

错误信息如下

来自数据源的 String 类型的给定值不能转换为指定目标列的类型 datetime。 ---> System.FormatException: 将参数值从 String 转换到 DateTime 失败。 ---> System.FormatException: 该字符串未被识别为有效的 DateTime。

我就开始检查这个表中的时间字段,结果检查了N次之后,发现所有插入表中的DateTime都是正确的。

后来在尝试其他方法后,发现由于我使用的DataTable中未包含自增长的列,加上自增列之后,就正确插入到数据库中了。

分析了一下,应该是列对应错误,

SqlBulkCopy不是根据表的ColumnName来匹配的,而是根据ColumnIndex匹配。所以插入数据的表需要和数据库中表的列名顺序完全一致

如果需要按照ColumnName对应来插入数据,可以指定SqlBulkCopyColumnMapping。代码如下

 SqlBulkCopy sbc = new SqlBulkCopy(conPrivate);

 foreach (DataColumn dc in dt.Columns)
sbc.ColumnMappings.Add(new SqlBulkCopyColumnMapping(dc.ColumnName, dc.ColumnName));

最新文章

  1. 云存储的那些事(2)——数据分布算法CRUSH
  2. php中关于js保存文件至本地的问题
  3. Android SDK的docs访问速度很慢(新)
  4. Hadoop学习4--安装Hadoop
  5. theano中的concolutional_mlp.py学习
  6. 【HDOJ】1422 重温世界杯
  7. lnmp下安装ffmpeg和ffmpeg-php教程
  8. Bogo排序
  9. php随机抽奖
  10. jQuery.extend 和 jQuery.fn.extend
  11. struts_自定义日期类型转换器
  12. Mysql bug: The server time zone value &#39;�й���׼ʱ��&#39; is unrecognized or represents more than one time zone.
  13. shiro多Realm第一次调用不生效问题
  14. query did not return a unique result: 2错误的发生
  15. json中带有\r\n处理
  16. 周强201771010141《面向对象程序设计Java》第八周学习总结
  17. mysql添加字段并且设置默认值
  18. AngularJS——第4章 数据绑定
  19. 问题解决:Apache: You don&#39;t have permission to access / on this server
  20. mysq 数据库基本管理

热门文章

  1. flutter Error:Cannot run with sound null safety, because the following dependencies don&#39;t support
  2. Codeforces Round #601 (Div. 2) A-E
  3. selenium注入js代码
  4. 什么是push通知栏消息?
  5. 微软出品自动化神器【Playwright+Java】系列(九)多线程、重定向、弹出新窗口、截图、新页面、录制、页面对象模式操作
  6. Spring Boot自动配置原理懂后轻松写一个自己的starter
  7. C++梳理
  8. pdf地址展示成Swiper轮播方式-复制链接
  9. URL带参数json传递进行解析
  10. 【译】.NET 7 中的性能改进(八)