public ResultInfo <int, string> BulkCopy(DbExecuteBulkParameter dbExecuteParameter) { int _times = 0; while (Interlocked.CompareExchange(ref signal, 1, 0) == 1) { Thread.Sleep(lockInterval); //waiting for lock to do; if (_times >= (timeoutLock + dbExecuteParameter.ExectueTimeout)) { return(ResultInfo <int, string> .Create(-1, "等待锁超时...")); } _times += (lockInterval / 1000); } try { Exception temex = null; int cnt = 0; using (DbCommonBulkCopy bulkCopy = CreateBulkCopy(DbConnectionString, dbExecuteParameter.IsTransaction)) { bulkCopy.Open(); bulkCopy.BulkCopiedHandler = dbExecuteParameter.BulkCopiedHandler; bulkCopy.BatchSize = dbExecuteParameter.BatchSize; bulkCopy.BulkCopyTimeout = dbExecuteParameter.ExectueTimeout; try { if ((dbExecuteParameter.DstDataTable == null || dbExecuteParameter.DstDataTable.Rows.Count == 0) && dbExecuteParameter.IDataReader != null) { bulkCopy.WriteToServer(dbExecuteParameter.IDataReader, dbExecuteParameter.DstTableName); cnt = 1; } else { if (dbExecuteParameter.BatchSize > dbExecuteParameter.DstDataTable.Rows.Count) { dbExecuteParameter.BatchSize = dbExecuteParameter.DstDataTable.Rows.Count; } bulkCopy.DestinationTableName = dbExecuteParameter.DstDataTable.TableName; bulkCopy.WriteToServer(dbExecuteParameter.DstDataTable); cnt = dbExecuteParameter.DstDataTable.Rows.Count; } //use transaction if (dbExecuteParameter.IsTransaction) { //有事务回调则由外边控制事务提交,否则直接提交事务 if (dbExecuteParameter.TransactionCallback != null) { dbExecuteParameter.TransactionCallback(bulkCopy.dbTrans, cnt); } else { bulkCopy.dbTrans.Commit(); } } } catch (Exception ex) { temex = ex; if (dbExecuteParameter.IsTransaction) { if (bulkCopy.dbTrans != null) { bulkCopy.dbTrans.Rollback(); } } } } if (temex != null) { throw temex; } return(ResultInfo <int, string> .Create(cnt, string.Empty)); } catch (Exception ex) { return(ResultInfo <int, string> .Create(-1, ex.ToString())); } finally { Interlocked.Exchange(ref signal, 0); } }