日期:2014-05-17 浏览次数:20799 次
该表在这儿只是演示使用,所以只提供了最基本的字段 现在往表中插入几条记录 insert into Counter select '127.0.0.1',getdate(),1 union all select '127.0.0.2',getdate(),1 union all select '127.0.0.3',getdate(),1
------解决方案--------------------
批量插入,每2000行提交一次
public void BulkCopy(string tableName, DataTable dt) { using (SqlConnection conn = new SqlConnection(_connString)) { conn.Open(); using (SqlTransaction transaction = conn.BeginTransaction()) { using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, transaction)) { bulkCopy.BatchSize = 2000; bulkCopy.BulkCopyTimeout = _CommandTimeOut; bulkCopy.DestinationTableName = tableName; try { foreach (DataColumn col in dt.Columns) { bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName); } bulkCopy.WriteToServer(dt); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); base.ThrowException(ex); } finally { conn.Close(); } } } } }
------解决方案--------------------
BulkCopy,BulkInsert都可以
面对大批量数据时蛮有效的