コード例 #1
0
        protected DbBulkCopy CreateBulkCopy(string ConnectionString, bool isTransaction = false)
        {
            if (dbBulkCopy != null)
            {
                dbBulkCopy.Dispose();
            }

            if (isTransaction)
            {
                dbBulkCopy = new DbBulkCopy(DbProviderType, ConnectionString, CreateConnection(ConnectionString));
            }
            else
            {
                dbBulkCopy = new DbBulkCopy(DbProviderType, ConnectionString);
            }

            return(dbBulkCopy);
        }
コード例 #2
0
        public ResultInfo <int, string> BulkCopy(DbExecuteBulkParameter dbExecuteParameter)
        {
            while (Interlocked.CompareExchange(ref signal, 1, 0) == 1)
            {
                Thread.Sleep(LockTimeout);
                //waiting for lock to do;
            }
            try
            {
                Exception temex = null;
                int       cnt   = 0;
                using (DbBulkCopy 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(new ResultInfo <int, string>(cnt, string.Empty));
            }
            catch (Exception ex)
            {
                return(new ResultInfo <int, string>(-1, ex.ToString()));
            }
            finally
            {
                Interlocked.Exchange(ref signal, 0);
            }
        }