首页 > 代码库 > SQL批量插入出现 类型转换错误
SQL批量插入出现 类型转换错误
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批量插入出现 类型转换错误