/// <summary>
        /// 插入数据。
        /// </summary>
        /// <param name="TableName">表名称。</param>
        /// <param name="data">包含要插入的数据的字典(字典的键必与数据库目标表的字段一致)。</param>
        /// <returns></returns>
        protected int Insert(string TableName, Dictionary <string, object> data)
        {
            List <FieldDescription> Fields = new List <FieldDescription>();
            List <object>           Values = new List <object>();

            foreach (KeyValuePair <string, object> item in data)
            {
                Fields.Add(td.Field(item.Key));
                if (item.Value == null)
                {
                    Values.Add(DBNull.Value);
                }
                else
                {
                    Values.Add(item.Value);
                }
            }
            DbCommandBuilder Command = new DbCommandBuilder();

            Command.Insert(fm.Table(TableName), Fields.ToArray()).Values(Values.ToArray());
            if (Trans == null)
            {
                return(Engine.DBA.ExecuteNoneQuery(Command));
            }
            else
            {
                return(Trans.DBA.ExecuteNoneQuery(Command));
            }
        }
Example #2
0
        public bool AddGroup(string Name)
        {
            DbCommandBuilder Command = new DbCommandBuilder();

            Command.Insert(fm.Table(TableName), td.Field("GroupName"))
            .Values(Name);
            int result = DataEngine.CurrentEngine.DBA.ExecuteNoneQuery(Command);

            return(result > 0);
        }
Example #3
0
        /// <summary>
        /// 判断并构建指定行被指示的提交命令。
        /// </summary>
        /// <param name="Row">被提交的行。</param>
        /// <param name="rowSubmission">对该行的处理指示。</param>
        /// <returns></returns>
        private DbCommandBuilder DataRowSubmissionCommand(DataRow Row, DataRowSubmission rowSubmission)
        {
            if (rowSubmission == null)
            {
                return(null);
            }
            // 开始构建命令。
            DbCommandBuilder Command = new DbCommandBuilder();

            switch (rowSubmission.Submission)
            {
            case QuickDataChangerSubmission.Delete:
                if (rowSubmission.Conditions != null && rowSubmission.Conditions.Count > 0)
                {
                    Command.Delete(Row.Table.TableName).Where(rowSubmission.Conditions.ToArray());
                }
                break;

            case QuickDataChangerSubmission.Update:
                if (rowSubmission.Conditions != null && rowSubmission.Conditions.Count > 0)
                {
                    List <IDescription> expressions = new List <IDescription>();
                    foreach (DataColumn c in Row.Table.Columns)
                    {
                        if (!NotInsertFieldsContains(rowSubmission, c.ColumnName))
                        {
                            expressions.Add(td.Field(c.ColumnName) == Row[c.ColumnName]);
                        }
                    }
                    Command.Update(Row.Table.TableName).Set(expressions.ToArray()).Where(rowSubmission.Conditions.ToArray());
                }
                break;

            case QuickDataChangerSubmission.Insert:
                List <FieldDescription> Fields = new List <FieldDescription>();
                List <object>           Values = new List <object>();
                foreach (DataColumn c in Row.Table.Columns)
                {
                    if (!NotInsertFieldsContains(rowSubmission, c.ColumnName))
                    {
                        Fields.Add(td.Field(c.ColumnName));
                        Values.Add(Row[c.ColumnName]);
                    }
                }
                Command.Insert(fm.Table(Row.Table.TableName), Fields.ToArray()).Values(Values.ToArray());
                break;

            default:
                break;
            }
            return(Command);
        }
        /// <summary>
        /// 向数据表中添加一条记录.
        /// </summary>
        /// <typeparam name="TEntity">数据表对应的实体类型.</typeparam>
        /// <param name="entity">实体对象.</param>
        /// <param name="trans">写事务的事务控制器.</param>
        /// <exception cref="Exception">当新增数据失败时引发该异常.</exception>
        protected void Add <TEntity>(TEntity entity, DBTransactionController trans = null) where TEntity : DataEntity, new()
        {
            if (entity == null)
            {
                return;
            }
            // 增加记录时过滤掉自增长字段.
            string[]             withOutFields  = GetWithOutFields(entity.GetType());
            EntityTableAttribute tableAttribute = GetTableAttribute();
            int result = 0;

            if (BatchProccesser == null) // 非同连接分批处理模式.
            {
                bool             InTransaction = trans != null;
                QuickDataChanger DC;
                if (InTransaction)
                {
                    DC = new QuickDataChanger(trans, Writer);
                }
                else
                {
                    DC = new QuickDataChanger(Writer);
                }
                result = DC.SaveToDataBase(tableAttribute.TableName, entity.ToDictionary(withOutFields), false);
                if (result == -1) // 若添加失败则向外界抛出异常.
                {
                    string Error;
                    if (InTransaction)
                    {
                        Error = (trans.DBA.Errors != null && trans.DBA.Errors.Count > 0) ? trans.DBA.Errors[0].Message : string.Format("新增:{0}是产生未知错误.", entity.GetType().Name);
                    }
                    else
                    {
                        Error = (Writer.DBA.Error == null) ? string.Format("新增:{0}是产生未知错误.", entity.GetType().Name) : Writer.DBA.Error.Message;
                    }
                    throw new Exception(Error);
                }
                else // 新增成功时更新实体中的自增长字段值.
                {
                    if (withOutFields.Length > 0)
                    {
                        if (InTransaction)
                        {
                            entity.SetValue(withOutFields[withOutFields.Length - 1], trans.DBA.SCOPE_IDENTITY, true);
                        }
                        else
                        {
                            entity.SetValue(withOutFields[withOutFields.Length - 1], Writer.DBA.SCOPE_IDENTITY, true);
                        }
                    }
                }
            }
            else // 在同一个连接的分批处理模式.
            {
                List <FieldDescription>     fields = new List <FieldDescription>();
                List <object>               values = new List <object>();
                Dictionary <string, object> data   = entity.ToDictionary(withOutFields);
                foreach (KeyValuePair <string, object> item in data)
                {
                    fields.Add(td.Field(item.Key));
                    values.Add(item.Value);
                }
                DbCommandBuilder cb = new DbCommandBuilder();
                cb.Insert(tableAttribute.TableName, fields.ToArray()).Values(values.ToArray());
                result = BatchProccesser.Commander.ExecuteNonQuery(cb);
                if (withOutFields.Length > 0 && result > 0)
                {
                    entity.SetValue(withOutFields[withOutFields.Length - 1], BatchProccesser.Commander.SCOPE_IDENTITY, true);
                }
            }
        }