Example #1
0
        /// <summary>
        /// 查询数据返回实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="idb">数据库连接上下文</param>
        /// <param name="sql">查询的sql语句</param>
        /// <param name="paras">查询的参数列表,例如WHERE条件中Id=@0,@0 为第一个参数</param>
        /// <returns>返回的实体对象列表</returns>
        public static IList <T> Query <T>(this IDbHelper idb, string sql, object[] paras) where T : class, new()
        {
            if (!select.IsMatch(sql))
            {
                sql = string.Format("SELECT * FROM {0} WHERE {1}", DALUtil.GetTableName <T>(), sql);
            }

            List <IDataParameter> list = new List <IDataParameter>();
            int i = 0;

            foreach (var item in paras)
            {
                var idp = idb.GetIDataParameter();
                idp.ParameterName = DALUtil.GetParameterPrefix(idb) + i++.ToString();
                idp.Value         = item;
                list.Add(idp);
            }
            if (idb.DBType == DataBaseType.Oracle)
            {
                sql = sql.Replace("@", ":");
            }
            using (var idr = idb.ExecuteReader(CommandType.Text, sql, list.ToArray()))
            {
                return(DALUtil.GetEntity <T>(idr));
            }
        }
Example #2
0
        /// <summary>
        /// jlb扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="idb"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public static IList <T> Query <T>(this IDbHelper idb, Query query) where T : class, new()
        {
            string sql = "SELECT * FROM {0} WHERE 1=1 {1}";

            sql = string.Format(sql, DALUtil.GetTableName <T>(), query.GetCondition(true));

            var dt = idb.ExecuteTable(sql);

            return(DALUtil.GetEntity <T>(dt));
        }
Example #3
0
        /// <summary>
        /// 查询数据并返回对象实体列表
        /// </summary>
        /// <typeparam name="T">对象实体</typeparam>
        /// <param name="idb">数据库连接上下文</param>
        /// <param name="query">查询Query对象</param>
        /// <param name="pageindex">查询页码</param>
        /// <param name="pagesize">每页数据量(0,表示不分页)</param>
        /// <param name="totalcount">记录总数,如果不获取记录总数返回0.</param>
        /// <param name="bl_getcount">是否获取记录总数</param>
        /// <returns>实体对象列表</returns>
        public static IList <T> Query <T>(this IDbHelper idb, Query query, int pageindex, int pagesize, out int totalcount, bool bl_getcount) where T : class, new()
        {
            string sql = "SELECT * FROM {0} WHERE 1=1 {1}";

            sql = string.Format(sql, DALUtil.GetTableName <T>(), query.GetCondition(true));

            var dt = Query(idb, sql, pageindex, pagesize, out totalcount, bl_getcount);

            return(DALUtil.GetEntity <T>(dt));
        }
Example #4
0
        /// <summary>
        /// 查询数据返回实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="idb">数据库连接上下文</param>
        /// <param name="sql">查询的sql语句</param>
        /// <returns>返回的实体对象列表</returns>
        public static IList <T> Query <T>(this IDbHelper idb, string sql) where T : class, new()
        {
            if (!select.IsMatch(sql))
            {
                sql = string.Format("SELECT * FROM {0} WHERE {1}", DALUtil.GetTableName <T>(), sql);
            }

            using (var idr = idb.ExecuteReader(sql))
            {
                return(DALUtil.GetEntity <T>(idr));
            }
        }
Example #5
0
        public static T GetEntityWithQuery <T>(this IDbHelper idb, Query query) where T : class, new()
        {
            string sql = "SELECT * FROM {0} WHERE 1=1 {1}";

            sql = string.Format(sql, DALUtil.GetTableName <T>(), query.GetCondition(false));

            var dt = idb.ExecuteTable(sql);

            if (dt.Rows.Count > 0)
            {
                return(DALUtil.GetEntity <T>(dt)[0]);
            }
            else
            {
                return(default(T));
            }
        }
Example #6
0
        /// <summary>
        /// 根据指定主键删除一个实体
        /// </summary>
        /// <typeparam name="T">实体泛型</typeparam>
        /// <param name="idb">数据库连接上下文</param>
        /// <param name="pk">主键列表,注意:主键列表的顺序必须与实体完全一致</param>
        /// <returns>返回影响的记录条数,如果该数据不存在返回0</returns>
        public static int Delete <T>(this IDbHelper idb, object[] pk) where T : class, new()
        {
            string tablename = DALUtil.GetTableName <T>();
            var    pks       = DALUtil.GetPKAttribute <T>();

            if (pks.Count != pk.Length)
            {
                throw new Exception("主键个数与传入参数不一致");
            }

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < pk.Length; i++)
            {
                sb.AppendFormat(" AND {0} = '{1}'", pks[i], pk[i]);
            }

            string sql = string.Format("DELETE FROM {0} WHERE 1=1{1}", tablename, sb.ToString());

            return(idb.ExecuteNonQuery(sql));
        }
Example #7
0
        /// <summary>
        /// 根据主键获取实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="idb">数据库连接上下文</param>
        /// <param name="pk">主键 例如id=1 写入 GetEntityById(1)</param>
        /// <returns>如果存在该实体,返回实体,否则返回null。</returns>
        /// <remarks>
        /// 对于多主键实体,主键的顺序必须是 实体的顺序否则系统会取不到正确的数据。
        ///
        /// </remarks>
        public static T GetEntityById <T>(this IDbHelper idb, params object[] pk) where T : class, new()
        {
            // 根据实体获取主键
            var pklist = DALUtil.GetPKAttribute <T>();

            if (pklist.Count != pk.Count())
            {
                // 实体的主键 个数如果不一致 系统自动抛出异常
                throw new Exception("主键设置个数不一致");
            }
            List <IDataParameter> list = new List <IDataParameter>();

            string where = "1=1";
            for (int i = 0; i < pklist.Count; i++)
            {
                where += " AND " + pklist[i] + " = " + DALUtil.GetParameterPrefix(idb) + i.ToString();
                var idp = idb.GetIDataParameter();
                idp.ParameterName = DALUtil.GetParameterPrefix(idb) + i.ToString();
                idp.Value         = pk[i];
                list.Add(idp);
            }

            var dt = idb.ExecuteTable(CommandType.Text, string.Format("SELECT * FROM {0} WHERE {1}", DALUtil.GetTableName <T>(), where), list.ToArray());

            if (dt.Rows.Count > 0)
            {
                return(DALUtil.GetEntity <T>(dt)[0]);
            }
            else
            {
                return(default(T));
            }
        }