예제 #1
0
        /// <summary>
        /// 根据条件删除,此删除方法,不支持分表数据的删除
        /// </summary>
        /// <typeparam name="T">要删除的类型</typeparam>
        /// <param name="where">删除条件</param>
        /// <param name="paras">参数</param>
        /// <returns>影响行数</returns>
        public int Delete <T>(string where, params object[] paras)
        {
            Type    type  = typeof(T);
            DBTable table = MapHelper.GetDBTable(type);
            DBSql   dbsql = DBSqlHelper.GetDBSql(table.Name, _dbContext.DataType);

            where = FormatWhereOrder(table, where);
            Command.CommandText = string.Format("delete from {0} {1}", table.Name, where.Length > 0 ? " where " + where : "");
            Command.CommandType = CommandType.Text;
            Command.Parameters.Clear();
            int i = 0;

            foreach (object obj in paras)
            {
                AddParameter(FormatParameterName("p" + (i++).ToString()), ParameterDirection.Input, obj);
            }
            return(Command.ExecuteNonQuery());
        }
예제 #2
0
        /// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="type">映射表类型</param>
        /// <param name="date">要写入表的拆分日期。注意该日期必须与Id中的日期对应。比如按年拆分的表,Id中的年份与该日期中的年份必须相同</param>
        public void Insert(object instance, Type type, DateTime date)
        {
            DBTable table = MapHelper.GetDBTable(type);

            //取主键值
            object primaryVal = null;

            if (table.PrimaryKey[0].DBPrimaryType != DBPrimaryType.Identity)
            {
                primaryVal = this.GetValue(table.PrimaryKey[0], instance);
                if (primaryVal == null)
                {
                    throw new MyDBException("新增对象,非自增长表主键不能为空");
                }
            }

            //取表名。如果是拆分表,则获取拆分表名
            string tbName = table.Name;

            if (table.SeparateType != SeparateType.None)
            {
                //如果传入时间为空,则取myId中的时间
                if (date == DateTime.MinValue)
                {
                    date = MyIdMake.GetMyIdDate(primaryVal);
                }

                //获取数据库表名
                tbName = TableSeparate.GetTableName(table, date);
            }

            //获取该数据库表 对应的DBSql中的Insert语句
            DBSql dbsql = DBSqlHelper.GetDBSql(tbName, _dbContext.DataType);

            if (string.IsNullOrEmpty(dbsql.InsertSql))//如果该表的新增语句为空,则生成该表的Insert语句
            {
                dbsql.InsertSql = GetInsertSql(table, tbName);
            }

            //将数据写入数据库
            Insert(instance, primaryVal, table, dbsql.InsertSql);
        }
예제 #3
0
        /// <summary>
        /// 修改对象,支持分表数据的修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <returns>受影响的行数</returns>
        public int Update <T>(T instance)
        {
            Type    type  = typeof(T);
            DBTable table = MapHelper.GetDBTable(type);

            //获取修改对象的主键
            object primaryVal = this.GetValue(table.PrimaryKey[0], instance);

            //获取主键对应的数据库表名
            string tbName = TableSeparate.GetTableName(table, primaryVal);

            DBSql dbsql = DBSqlHelper.GetDBSql(tbName, _dbContext.DataType);

            if (string.IsNullOrEmpty(dbsql.UpdateSql))//如果该表的修改语句为空,则生成该表的update语句
            {
                dbsql.UpdateSql = GetUpdateSql(table, tbName);
            }

            //修改数据
            int retVal = Update <T>(instance, table, dbsql.UpdateSql, primaryVal);

            return(retVal);
        }
예제 #4
0
        /// <summary>
        /// 根据主键删除,支持分表数据的删除
        /// </summary>
        /// <typeparam name="T">要删除的类型</typeparam>
        /// <typeparam name="IdT">主键类型</typeparam>
        /// <param name="id">主键值</param>
        /// <returns>影响行数</returns>
        public int Delete <T, IdT>(IdT id)
        {
            Type    type  = typeof(T);
            DBTable table = MapHelper.GetDBTable(type);

            if (table.PrimaryKey.Count > 1)
            {
                throw new Exception("联合主键表,不支持根据主键删除");
            }

            //获取对应的数据库表名
            string tbName = TableSeparate.GetTableName(table, id);

            DBSql dbsql = DBSqlHelper.GetDBSql(tbName, _dbContext.DataType);

            if (string.IsNullOrEmpty(dbsql.DeleteSql))//如果该表的修改语句为空,则生成该表的update语句
            {
                dbsql.DeleteSql = GetDeleteSql(table, tbName);
            }

            int retVal = Delete <T, IdT>(id, table, dbsql.DeleteSql);

            return(retVal);
        }