码迷,mamicode.com
首页 > 数据库 > 详细

SQL批量插入出现 类型转换错误

时间:2017-03-21 19:50:39      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:ntc   code   ati   manage   bat   批量操作   car   except   建立   

1、原因:在使用SqlBulkCopy批量操作时,Map映射会出现表结点对应错误

2、解决方案:自己先建立字段映射

using (SqlConnection con = new SqlConnection(DbConfig.CsNisConnString))
{
con.Open();
using (SqlTransaction tran = con.BeginTransaction())
{
SqlBulkCopy bc = new SqlBulkCopy(con,
SqlBulkCopyOptions.CheckConstraints |
SqlBulkCopyOptions.FireTriggers |
SqlBulkCopyOptions.KeepNulls, tran);

SqlBulkCopyColumnMapping m1 = new SqlBulkCopyColumnMapping("SchoolId", "SchoolId");
SqlBulkCopyColumnMapping m2 = new SqlBulkCopyColumnMapping("AreaCode", "AreaCode");
SqlBulkCopyColumnMapping m3 = new SqlBulkCopyColumnMapping("LastPushDate", "LastPushDate");
SqlBulkCopyColumnMapping m4 = new SqlBulkCopyColumnMapping("StudentCode", "StudentCode");
SqlBulkCopyColumnMapping m5 = new SqlBulkCopyColumnMapping("StudentName", "StudentName");
SqlBulkCopyColumnMapping m6 = new SqlBulkCopyColumnMapping("ClassCode", "ClassCode");
SqlBulkCopyColumnMapping m7 = new SqlBulkCopyColumnMapping("ClassName", "ClassName");
SqlBulkCopyColumnMapping m8 = new SqlBulkCopyColumnMapping("ManageCode", "ManageCode");
SqlBulkCopyColumnMapping m9 = new SqlBulkCopyColumnMapping("UnPushCount", "UnPushCount");

bc.ColumnMappings.Add(m1);
bc.ColumnMappings.Add(m2);
bc.ColumnMappings.Add(m3);
bc.ColumnMappings.Add(m4);
bc.ColumnMappings.Add(m5);
bc.ColumnMappings.Add(m6);
bc.ColumnMappings.Add(m7);
bc.ColumnMappings.Add(m8);
bc.ColumnMappings.Add(m9);

bc.BatchSize = 10000;
bc.DestinationTableName = "XCard_PclException"; //目标表
bc.WriteToServer(listAll.AsDataReader());
tran.Commit();
}
con.Close();
}

SQL批量插入出现 类型转换错误

标签:ntc   code   ati   manage   bat   批量操作   car   except   建立   

原文地址:http://www.cnblogs.com/xuqy/p/6596146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!