コード例 #1
0
        internal bool MsSqlBulkCopyInsert(bool keepID)
        {
            SqlTransaction sqlTran     = null;
            SqlConnection  con         = null;
            bool           isCreateDal = false;

            try
            {
                CheckGUIDAndDateTime(DalType.MsSql);
                string conn = AppConfig.GetConn(_Conn);
                if (_dalHelper == null)
                {
                    if (IsTruncate)
                    {
                        isCreateDal = true;
                        _dalHelper  = DalCreate.CreateDal(conn);
                    }
                    else
                    {
                        con = new SqlConnection(conn);
                        con.Open();
                    }
                }
                bool isGoOn = true;
                if (_dalHelper != null)
                {
                    if (IsTruncate)
                    {
                        _dalHelper.isOpenTrans = true;
                        if (_dalHelper.ExeNonQuery(string.Format(SqlCreate.TruncateTable, SqlFormat.Keyword(mdt.TableName, dalTypeTo)), false) == -2)
                        {
                            isGoOn = false;
                            sourceTable.DynamicData = _dalHelper.debugInfo;
                            Log.WriteLogToTxt(_dalHelper.debugInfo.ToString());
                        }
                    }
                    if (isGoOn)
                    {
                        con = _dalHelper.Con as SqlConnection;
                        _dalHelper.OpenCon(null, AllowConnLevel.MasterBackup);//如果未开启,则开启,打开链接后,如果以前没执行过数据,事务对象为空,这时会产生事务对象
                        sqlTran = _dalHelper._tran as SqlTransaction;
                    }
                }
                if (isGoOn)
                {
                    using (SqlBulkCopy sbc = new SqlBulkCopy(con, (keepID ? SqlBulkCopyOptions.KeepIdentity : SqlBulkCopyOptions.Default) | SqlBulkCopyOptions.FireTriggers, sqlTran))
                    {
                        sbc.BatchSize            = 100000;
                        sbc.DestinationTableName = SqlFormat.Keyword(mdt.TableName, DalType.MsSql);
                        sbc.BulkCopyTimeout      = AppConfig.DB.CommandTimeout;
                        foreach (MCellStruct column in mdt.Columns)
                        {
                            sbc.ColumnMappings.Add(column.ColumnName, column.ColumnName);
                        }
                        if (AppConfig.IsAspNetCore)
                        {
                            sbc.WriteToServer(mdt.ToDataTable());
                        }
                        else
                        {
                            sbc.WriteToServer(mdt);
                        }
                    }
                }
                return(true);
            }
            catch (Exception err)
            {
                sourceTable.DynamicData = err;
                Log.WriteLogToTxt(err);
            }
            finally
            {
                if (_dalHelper == null)
                {
                    con.Close();
                    con = null;
                }
                else if (isCreateDal)
                {
                    _dalHelper.EndTransaction();
                    _dalHelper.Dispose();
                }
            }
            return(false);
        }