internal bool NormalDelete()
        {
            bool result = true;

            using (MAction action = new MAction(mdt.TableName, _Conn))
            {
                action.SetAopState(Aop.AopOp.CloseAll);
                DalBase sourceHelper = action.dalHelper;
                if (_dalHelper != null)
                {
                    action.dalHelper = _dalHelper;
                }
                else
                {
                    action.BeginTransation();
                }
                action.dalHelper.IsRecordDebugInfo = false;//屏蔽SQL日志记录

                MDataRow row;
                for (int i = 0; i < mdt.Rows.Count; i++)
                {
                    row = mdt.Rows[i];
                    action.ResetTable(row, false);
                    string where = SqlCreate.GetWhere(action.DataBaseType, GetJoinPrimaryCell(row));
                    result       = action.Delete(where) || action.RecordsAffected == 0;//没有可更新的数据,也返回true
                    if (action.RecordsAffected > 0)
                    {
                        sourceTable.RecordsAffected += action.RecordsAffected;//记录总删除的数量。
                    }
                    if (!result)
                    {
                        sourceTable.RecordsAffected = 0;
                        string msg = "Error On : MDataTable.AcceptChanges.Delete." + mdt.TableName + " : where (" + where + ") : " + action.DebugInfo;
                        sourceTable.DynamicData = msg;
                        Log.Write(msg, LogType.DataBase);
                        break;
                    }
                }
                action.dalHelper.IsRecordDebugInfo = true;//恢复SQL日志记录
                if (_dalHelper == null)
                {
                    action.EndTransation();
                }
                else
                {
                    action.dalHelper = sourceHelper;//恢复原来,避免外来的链接被关闭。
                }
            }

            return(result);
        }
        internal bool Auto()
        {
            bool result = true;

            using (MAction action = new MAction(mdt.TableName, _Conn))
            {
                action.SetAopState(Aop.AopOp.CloseAll);
                DalBase sourceHelper = action.dalHelper;
                if (_dalHelper != null)
                {
                    action.dalHelper = _dalHelper;
                }
                else
                {
                    action.BeginTransation();
                }
                action.dalHelper.IsRecordDebugInfo = false;//屏蔽SQL日志记录 2000数据库大量的In条件会超时。

                if ((jointPrimaryIndex != null && jointPrimaryIndex.Count == 1) || (jointPrimaryIndex == null && mdt.Columns.JointPrimary.Count == 1))
                //jointPrimaryIndex == null && mdt.Columns.JointPrimary.Count == 1 && mdt.Rows.Count <= 10000
                //&& (!action.DalVersion.StartsWith("08") || mdt.Rows.Count < 1001)) //只有一个主键-》组合成In远程查询返回数据-》
                {
                    #region 新逻辑

                    MCellStruct keyColumn  = jointPrimaryIndex != null ? mdt.Columns[jointPrimaryIndex[0]] : mdt.Columns.FirstPrimary;
                    string      columnName = keyColumn.ColumnName;
                    //计算分组处理
                    int pageSize = 5000;
                    if (action.DataBaseVersion.StartsWith("08"))
                    {
                        pageSize = 1000;
                    }
                    int count = mdt.Rows.Count / pageSize + 1;
                    for (int i = 0; i < count; i++)
                    {
                        MDataTable dt = mdt.Select(i + 1, pageSize, null);//分页读取
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            string whereIn = SqlCreate.GetWhereIn(keyColumn, dt.GetColumnItems <string>(columnName, BreakOp.NullOrEmpty, true), action.DataBaseType);
                            action.SetSelectColumns(columnName);
                            MDataTable keyTable = action.Select(whereIn);                                                                                                             //拿到数据,准备分拆上市

                            MDataTable[] dt2 = dt.Split(SqlCreate.GetWhereIn(keyColumn, keyTable.GetColumnItems <string>(columnName, BreakOp.NullOrEmpty, true), DataBaseType.None)); //这里不需要格式化查询条件。
                            result = dt2[0].Rows.Count == 0;
                            if (!result)
                            {
                                MDataTable updateTable = dt2[0];
                                updateTable.SetState(2, BreakOp.Null);
                                updateTable.DynamicData = action;
                                result = updateTable.AcceptChanges(AcceptOp.Update, _Conn, columnName);
                                if (!result)
                                {
                                    sourceTable.DynamicData = updateTable.DynamicData;
                                }
                            }
                            if (result && dt2[1].Rows.Count > 0)
                            {
                                MDataTable insertTable = dt2[1];
                                insertTable.DynamicData = action;
                                bool keepid = !insertTable.Rows[0].PrimaryCell.IsNullOrEmpty;
                                result = insertTable.AcceptChanges((keepid ? AcceptOp.InsertWithID : AcceptOp.Insert), _Conn, columnName);
                                if (!result)
                                {
                                    sourceTable.DynamicData = insertTable.DynamicData;
                                }
                            }
                        }
                    }

                    #endregion

                    #region 旧逻辑,已不用 分拆处理 本地比较分拆两个表格【更新和插入】-》分开独立处理。

                    /*
                     * string columnName = mdt.Columns.FirstPrimary.ColumnName;
                     * string whereIn = SqlCreate.GetWhereIn(mdt.Columns.FirstPrimary, mdt.GetColumnItems<string>(columnName, BreakOp.NullOrEmpty, true), action.DalType);
                     * action.SetSelectColumns(mdt.Columns.FirstPrimary.ColumnName);
                     * dt = action.Select(whereIn);
                     *
                     * MDataTable[] dt2 = mdt.Split(SqlCreate.GetWhereIn(mdt.Columns.FirstPrimary, dt.GetColumnItems<string>(columnName, BreakOp.NullOrEmpty, true), DalType.None));//这里不需要格式化查询条件。
                     * result = dt2[0].Rows.Count == 0;
                     * if (!result)
                     * {
                     *  dt2[0].SetState(2, BreakOp.Null);
                     *  dt2[0].DynamicData = action;
                     *  MDataTableBatchAction m1 = new MDataTableBatchAction(dt2[0], _Conn);
                     *  m1.SetJoinPrimaryKeys(new string[] { columnName });
                     *  result = m1.Update();
                     *  if (!result)
                     *  {
                     *      sourceTable.DynamicData = dt2[0].DynamicData;
                     *  }
                     * }
                     * if (result && dt2[1].Rows.Count > 0)
                     * {
                     *  dt2[1].DynamicData = action;
                     *  MDataTableBatchAction m2 = new MDataTableBatchAction(dt2[1], _Conn);
                     *  m2.SetJoinPrimaryKeys(new string[] { columnName });
                     *  result = m2.Insert(!dt2[1].Rows[0].PrimaryCell.IsNullOrEmpty);
                     *  if (!result)
                     *  {
                     *      sourceTable.DynamicData = dt2[1].DynamicData;
                     *  }
                     * }
                     */
                    #endregion
                }
                else
                {
                    // action.BeginTransation();
                    foreach (MDataRow row in mdt.Rows)
                    {
                        #region 循环处理
                        action.ResetTable(row, false);
                        string where = SqlCreate.GetWhere(action.DataBaseType, GetJoinPrimaryCell(row));
                        bool isExists = action.Exists(where);
                        if (action.RecordsAffected == -2)
                        {
                            result = false;
                        }
                        else
                        {
                            if (!isExists)
                            {
                                action.AllowInsertID = !row.PrimaryCell.IsNullOrEmpty;
                                action.Data.SetState(1, BreakOp.Null);
                                result = action.Insert(InsertOp.None);
                            }
                            else
                            {
                                action.Data.SetState(2);
                                result = action.Update(where);
                            }
                        }
                        if (!result)
                        {
                            string msg = "Error On : MDataTable.AcceptChanges.Auto." + mdt.TableName + " : [" + where + "] : " + action.DebugInfo;
                            sourceTable.DynamicData = msg;
                            Log.Write(msg, LogType.DataBase);
                            break;
                        }
                        #endregion
                    }
                }
                action.dalHelper.IsRecordDebugInfo = true;//恢复SQL日志记录
                if (_dalHelper == null)
                {
                    action.EndTransation();
                }
                else
                {
                    action.dalHelper = sourceHelper;//还原
                }
            }

            return(result);
        }
예제 #3
0
        internal bool NormalUpdate()
        {
            List <int> indexList = new List <int>();
            bool       result    = true;

            using (MAction action = new MAction(mdt.TableName, _Conn))
            {
                action.SetAopState(Aop.AopOp.CloseAll);
                DbBase sourceHelper = action.dalHelper;
                if (_dalHelper != null)
                {
                    action.dalHelper = _dalHelper;
                }
                else
                {
                    action.BeginTransation();
                }
                action.dalHelper.IsAllowRecordSql = false;//屏蔽SQL日志记录

                MDataRow row;
                for (int i = 0; i < mdt.Rows.Count; i++)
                {
                    row = mdt.Rows[i];
                    if (row.GetState(true) > 1)
                    {
                        action.ResetTable(row, false);
                        string where = SqlCreate.GetWhere(action.DalType, GetJoinPrimaryCell(row));
                        result       = action.Update(where) || action.RecordsAffected == 0;//没有可更新的数据,也返回true
                        if (action.RecordsAffected > 0)
                        {
                            sourceTable.RecordsAffected += action.RecordsAffected;//记录总更新的数量。
                        }
                        if (!result)
                        {
                            sourceTable.RecordsAffected = 0;
                            string msg = "Error On : MDataTable.AcceptChanges.Update." + mdt.TableName + " : where (" + where + ") : " + action.DebugInfo;
                            sourceTable.DynamicData = msg;
                            Log.WriteLogToTxt(msg);
                            break;
                        }
                        else
                        {
                            indexList.Add(i);
                        }
                    }
                }
                action.dalHelper.IsAllowRecordSql = true;//恢复SQL日志记录
                if (_dalHelper == null)
                {
                    action.EndTransation();
                }
                else
                {
                    action.dalHelper = sourceHelper;//恢复原来,避免外来的链接被关闭。
                }
            }
            if (result)
            {
                foreach (int index in indexList)
                {
                    mdt.Rows[index].SetState(0);
                }
                indexList.Clear();
                indexList = null;
            }
            return(result);
        }