Exemple #1
0
        /// <summary>
        /// 通过ID获取指定类型的对象,支持分表数据的获取
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type">返回对象类型</param>
        /// <returns></returns>
        public object GetById(object id, Type type)
        {
            DBTable table = MapHelper.GetDBTable(type);

            if (table.PrimaryKey.Count > 1)
            {
                throw new Exception("联合主建,不支持GetById方法");
            }

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

            string pName = this.FormatParameterName(table.PrimaryKey[0].AliasName);
            string sql   = string.Format("select * from {0} where {1}={2}", tbName, table.PrimaryKey[0].Name, pName);

            Command.Parameters.Clear();
            //添加查询条件参数
            AddParameter(this.FormatParameterName(table.PrimaryKey[0].AliasName), ParameterDirection.Input, id);
            Command.CommandText = sql;
            Command.CommandType = CommandType.Text;

            using (IDataReader reader = Command.ExecuteReader())
            {
                while (reader.Read())
                {
                    return(FullDataReader.CreateDegFullMapObj(reader, type)(reader));
                }
            }
            return(null);
        }
Exemple #2
0
        /// <summary>
        /// 根据条件查询对象,不支持分表数据的查询
        /// 只支持映射类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public T GetObject <T>(string where, params object[] paras)
        {
            Type    type  = typeof(T);
            DBTable table = MapHelper.GetDBTable(type);

            string sql = string.Format("select * from {0} {1}",
                                       table.Name,
                                       string.IsNullOrEmpty(where) ? "" : "where " + FormatWhereOrder(table, where));

            Command.CommandText = sql;
            Command.CommandType = CommandType.Text;
            Command.Parameters.Clear();

            int i = 0;

            foreach (object obj in paras)
            {
                AddParameter(FormatParameterName("p" + (i++).ToString()), ParameterDirection.Input, obj);
            }
            using (IDataReader reader = Command.ExecuteReader())
            {
                while (reader.Read())
                {
                    return(FullDataReader.CreateDegFullMapObj <T>(reader)(reader));
                }
            }
            return(default(T));
        }
Exemple #3
0
        /// <summary>
        /// 根据SQL语句查询对象,不支持分表数据的查询
        /// </summary>
        /// <param name="where"></param>
        /// <param name="type">返回对象类型</param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public object GetObjectBySQL(string sql, Type type, params object[] paras)
        {
            DBTable table = MapHelper.GetDBTableExist(type);

            if (table != null)
            {
                Command.CommandText = FormatWhereOrder(table, sql);
                Command.CommandType = CommandType.Text;
                Command.Parameters.Clear();
                int i = 0;
                foreach (object obj in paras)
                {
                    AddParameter(FormatParameterName("p" + (i++).ToString()), ParameterDirection.Input, obj);
                }
                using (IDataReader reader = Command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        return(FullDataReader.CreateDegFullMapObj(reader, type)(reader));
                    }
                }
                return(null);
            }
            else
            {
                return(GetCustomerObject(sql, type, paras));
            }
        }