标签:
public bool CreateTB_PROPERTY_MODELLByList(List<TB_PROPERTY_MODEL> entity) { try { if (entity.Count > 0) { string strsql = "delete from TB_PROPERTY_MODEL where DocumentModel=‘" + entity[0].DocumentModel + "‘"; _dbContext.Database.ExecuteSqlCommand(strsql); } //将集合转换成DataTable DataTable dtTable = Untity.ListToDataTable(entity); var sqlConn = (SqlConnection)_dbContext.Database.Connection; ; sqlConn.Open(); using (SqlTransaction transaction = sqlConn.BeginTransaction()) { using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn, SqlBulkCopyOptions.Default, transaction)) { bulkCopy.BatchSize = 10000; bulkCopy.BulkCopyTimeout = 600; bulkCopy.DestinationTableName = "TB_PROPERTY_MODEL"; try {
//这里列一定要和数据库中的表的列要一样.和数据类型也一样。不然无法导入 foreach (DataColumn col in dtTable.Columns) { bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName); } bulkCopy.WriteToServer(dtTable); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); LogHelper.Error(ex.Message); throw ex.InnerException; } finally { bulkCopy.Close(); sqlConn.Close(); } } } return true; } catch (EntityException ex) { LogHelper.Error(ex.Message); throw ex.InnerException; } catch (DbException ex) { LogHelper.Error(ex.Message); throw ex.InnerException; } }
之前用的是EF的数据写入。感觉EF在写入的时候有点慢,可能是自己没有找到EF正确的方法。
标签:
原文地址:http://www.cnblogs.com/w2011/p/4613130.html