/// <summary>
        /// 删除数据表中满足指定条件的数据.
        /// </summary>
        /// <param name="func">用于创建删除条件.</param>
        /// <exception cref="Exception">当未指定更新条件或删除过程产生错误时引发此异常.</exception>
        /// <returns>返回受影响记录数.</returns>
        public int Delete(Func <TEntityAgent, object[]> func)
        {
            if (func == null)
            {
                throw new Exception("在不指定条件的情况下将会删除整个数据表中的所有数据,该操作已被禁止.");
            }
            EntityTableAttribute tableAttribute = GetTableAttribute();
            TEntityAgent         agent          = new TEntityAgent();

            agent.TableContext = this;
            object[] conditions = func(agent);
            int      result     = 0;

            if (BatchProccesser == null)
            {
                QuickDataChanger DC = new QuickDataChanger(Writer);
                DC.Conditions.AddRange(conditions);
                result = DC.Delete(tableAttribute.TableName);
                if (result == -1)
                {
                    throw new Exception(Writer.DBA.Error.Message);
                }
            }
            else
            {
                DbCommandBuilder cb = new DbCommandBuilder();
                cb.Delete(tableAttribute.TableName).Where(conditions);
                result = BatchProccesser.Commander.ExecuteNonQuery(cb);
            }
            return(result);
        }
        /// <summary>
        /// 在事务中删除数据表中满足指定条件的数据.
        /// </summary>
        /// <param name="trans">写事务的事务控制器.</param>
        /// <param name="func">用于创建删除条件.</param>
        /// <exception cref="ArgumentNullException">当未指派事务控制器时引发此异常.</exception>
        /// <exception cref="Exception">当未指定更新条件或删除过程产生错误时引发此异常.</exception>
        /// <returns></returns>
        public int Delete(DBTransactionController trans, Func <TEntityAgent, object[]> func)
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }
            if (func == null)
            {
                throw new Exception("在不指定条件的情况下将会删除整个数据表中的所有数据,该操作已被禁止.");
            }
            EntityTableAttribute tableAttribute = GetTableAttribute();
            QuickDataChanger     DC             = new QuickDataChanger(trans, Writer);
            TEntityAgent         agent          = new TEntityAgent();

            agent.TableContext = this;
            DC.Conditions.AddRange(func(agent));
            trans.DBA.Errors.Clear();
            int result = DC.Delete(tableAttribute.TableName);

            if (result == -1)
            {
                if (trans.DBA.Errors != null && trans.DBA.Errors.Count > 0)
                {
                    throw new Exception(trans.DBA.Errors[0].Message);
                }
                else
                {
                    throw new Exception("删除数据是产生了未知的错误.");
                }
            }
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// 更新数据表中的记录.
        /// </summary>
        /// <typeparam name="TEntity">数据表对应的实体类型.</typeparam>
        /// <param name="entity">实体对象.</param>
        /// <param name="func">用于创建更新条件.</param>
        /// <exception cref="ArgumentNullException">当必备的参数为空时引发此异常.</exception>
        /// <exception cref="Exception">当执行更新时产生错误时引发此异常.</exception>
        /// <returns>返回受影响的记录数.</returns>
        protected int Update <TEntity>(TEntity entity, Func <TEntityAgent, object[]> func) where TEntity : DataEntity, new()
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (func == null)
            {
                throw new Exception("未指定更新条件时,整个数据表中的所有数据都将被更新为一样的,此操作已被禁止.");
            }
            // 过滤掉自增长字段.
            string[]             withOutFields  = GetWithOutFields(entity.GetType());
            EntityTableAttribute tableAttribute = GetTableAttribute();
            QuickDataChanger     DC             = new QuickDataChanger(Writer);

            if (func != null)
            {
                TEntityAgent agent = new TEntityAgent();
                agent.TableContext = this;
                DC.Conditions.AddRange(func(agent));
            }
            int result = DC.SaveToDataBase(tableAttribute.TableName, entity.ToDictionary(withOutFields), true);

            if (result == -1) // 若更新失败时向外界抛出异常.
            {
                throw new Exception(Writer.DBA.Error.Message);
            }
            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// 从指定的表中删除指定条件的数据,并返回受影响的记录数.
        /// </summary>
        /// <param name="tableName">表名称.</param>
        /// <param name="condition">删除条件.</param>
        /// <param name="trans">在该事务中执行更新,若无事务则为 null.</param>
        /// <returns></returns>
        public virtual int Delete(string tableName, object[] condition, DBTransactionController trans = null)
        {
            QuickDataChanger dc = new QuickDataChanger(trans, db);

            dc.Conditions.AddRange(condition);
            int result = dc.Delete(tableName);

            ThrowDbException(result, trans);
            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// 更新数据,并返回受影响的记录数.
        /// </summary>
        /// <param name="data">包含要更新的数据的字典.</param>
        /// <param name="tableName">表名称.</param>
        /// <param name="condition">更新条件.</param>
        /// <param name="trans">在该事务中执行更新,若无事务则为 null.</param>
        /// <returns></returns>
        public virtual int Update(Dictionary <string, object> data, string tableName, object[] condition, DBTransactionController trans = null)
        {
            QuickDataChanger dc = new QuickDataChanger(trans, db);

            dc.Conditions.AddRange(condition);
            int result = dc.SaveToDataBase(tableName, data, true);

            ThrowDbException(result, trans);
            return(result);
        }
Beispiel #6
0
        protected bool UpdateData(Dictionary <string, object> data, DBTransactionController trans = null)
        {
            QuickDataChanger QuickChange = new QuickDataChanger(trans, DataEngine.CurrentEngine);

            QuickChange.Conditions.Add(td.Field("TestId") == data["TestId"]);
            data.Remove("TestId"); // TestId 传进来作为条件,不更新它(所以这里要从字典中删除)。
            int result = QuickChange.SaveToDataBase(TABLE_NAME, data, true);

            return(result > 0);
        }
Beispiel #7
0
        /// <summary>
        /// 添加数据,返回自增长编号字段的值.
        /// </summary>
        /// <param name="data">包含添加的数据的对象.</param>
        /// <param name="tableName">表名称.</param>
        /// <param name="trans">在该事务中执行添加任务,若无事务则为 null.</param>
        /// <returns></returns>
        public virtual object Add(Dictionary <string, object> data, string tableName, DBTransactionController trans = null)
        {
            QuickDataChanger dc = new QuickDataChanger(trans, db);

            ThrowDbException(dc.SaveToDataBase(tableName, data, false), trans);
            if (trans != null)
            {
                return(trans.DBA.SCOPE_IDENTITY);
            }
            return(db.DBA.SCOPE_IDENTITY);
        }
Beispiel #8
0
        /// <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());
            // 向数据库中增加记录
            bool InTransaction = trans != null;
            EntityTableAttribute tableAttribute = GetTableAttribute();
            QuickDataChanger     DC;

            if (InTransaction)
            {
                DC = new QuickDataChanger(trans, Writer);
            }
            else
            {
                DC = new QuickDataChanger(Writer);
            }
            int 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);
                    }
                }
            }
        }
        /// <summary>
        /// 更新数据表中的记录.
        /// </summary>
        /// <typeparam name="TEntity">数据表对应的实体类型.</typeparam>
        /// <param name="entity">实体对象.</param>
        /// <param name="func">用于创建更新条件.</param>
        /// <exception cref="ArgumentNullException">当必备的参数为空时引发此异常.</exception>
        /// <exception cref="Exception">当执行更新时产生错误时引发此异常.</exception>
        /// <returns>返回受影响的记录数.</returns>
        protected int Update <TEntity>(TEntity entity, Func <TEntityAgent, object[]> func) where TEntity : DataEntity, new()
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (func == null)
            {
                throw new Exception("未指定更新条件时,整个数据表中的所有数据都将被更新为一样的,此操作已被禁止.");
            }
            // 过滤掉自增长字段.
            string[]             withOutFields  = GetWithOutFields(entity.GetType());
            EntityTableAttribute tableAttribute = GetTableAttribute();

            object[] conditions = null;
            if (func != null)  // 获得外部 lambda 表达式返回的条件.
            {
                TEntityAgent agent = new TEntityAgent();
                agent.TableContext = this;
                conditions         = func(agent);
            }
            int result = 0;

            if (BatchProccesser == null)  //非同连接分批处理模式.
            {
                QuickDataChanger DC = new QuickDataChanger(Writer);
                if (conditions != null)
                {
                    DC.Conditions.AddRange(conditions);
                }
                result = DC.SaveToDataBase(tableAttribute.TableName, entity.ToDictionary(withOutFields), true);
                if (result == -1) // 若更新失败时向外界抛出异常.
                {
                    throw new Exception(Writer.DBA.Error.Message);
                }
            }
            else  //在同一个连接的分批处理模式.
            {
                List <IDescription>         exp  = new List <IDescription>();
                Dictionary <string, object> data = entity.ToDictionary(withOutFields);
                foreach (KeyValuePair <string, object> item in data)
                {
                    exp.Add(td.Field(item.Key) == item.Value);
                }
                DbCommandBuilder cb = new DbCommandBuilder();
                cb.Update(tableAttribute.TableName).Set(exp.ToArray()).Where(conditions);
                result = BatchProccesser.Commander.ExecuteNonQuery(cb);
            }
            return(result);
        }
Beispiel #10
0
        protected bool AddData(Dictionary <string, object> data, DBTransactionController trans = null)
        {
            if (data == null)
            {
                return(false);
            }
            if (!data.ContainsKey("TestPhoto"))
            {
                data.Add("TestPhoto", DBNull.Value);
            }
            QuickDataChanger QuickChange = new QuickDataChanger(trans, DataEngine.CurrentEngine);
            int result = QuickChange.SaveToDataBase(TABLE_NAME, data, false);

            return(result > 0);
        }
Beispiel #11
0
        /// <summary>
        /// 在事务中更新数据表中的记录.
        /// </summary>
        /// <typeparam name="TEntity">数据表对应的实体类型.</typeparam>
        /// <param name="trans">写事务的事务控制器.</param>
        /// <param name="entity">实体对象.</param>
        /// <param name="func">用于创建更新条件.</param>
        /// <exception cref="ArgumentNullException">当必备的参数为空时引发此异常.</exception>
        /// <exception cref="Exception">当执行更新时产生错误时引发此异常.</exception>
        /// <returns>返回受影响的记录数.</returns>
        protected int Update <TEntity>(DBTransactionController trans, TEntity entity, Func <TEntityAgent, object[]> func) where TEntity : DataEntity, new()
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (func == null)
            {
                throw new Exception("未指定更新条件时,整个数据表中的所有数据都将被更新为一样的,此操作已被禁止.");
            }
            // 过滤掉自增长字段.
            // 过滤掉自增长字段.
            string[]             withOutFields  = GetWithOutFields(entity.GetType());
            EntityTableAttribute tableAttribute = GetTableAttribute();
            QuickDataChanger     DC             = new QuickDataChanger(trans, Writer);

            if (func != null)
            {
                TEntityAgent agent = new TEntityAgent();
                agent.TableContext = this;
                DC.Conditions.AddRange(func(agent));
            }
            trans.DBA.Errors.Clear();
            int result = DC.SaveToDataBase(tableAttribute.TableName, entity.ToDictionary(withOutFields), true);

            if (result == -1) // 若更新失败时向外界抛出异常.
            {
                string Error;
                if (trans.DBA.Errors.Count < 1)
                {
                    Error = "更新表时产生了未知的异常.";
                }
                else
                {
                    Error = trans.DBA.Errors[0].Message;
                }
                throw new Exception(Error);
            }
            return(result);
        }
Beispiel #12
0
        /// <summary>
        /// 删除数据表中满足指定条件的数据.
        /// </summary>
        /// <param name="func">用于创建删除条件.</param>
        /// <exception cref="Exception">当未指定更新条件或删除过程产生错误时引发此异常.</exception>
        /// <returns>返回受影响记录数.</returns>
        public int Delete(Func <TEntityAgent, object[]> func)
        {
            if (func == null)
            {
                throw new Exception("在不指定条件的情况下将会删除整个数据表中的所有数据,该操作已被禁止.");
            }
            EntityTableAttribute tableAttribute = GetTableAttribute();
            QuickDataChanger     DC             = new QuickDataChanger(Writer);
            TEntityAgent         agent          = new TEntityAgent();

            agent.TableContext = this;
            DC.Conditions.AddRange(func(agent));
            int result = DC.Delete(tableAttribute.TableName);

            if (result == -1)
            {
                throw new Exception(Writer.DBA.Error.Message);
            }
            return(result);
        }
        /// <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);
                }
            }
        }