ORM 执行SQL语句查询 的两种方式

BaseRepository

/// <summary>
/// 获取SQL执行帮助类实例
/// </summary>
protected IDbHelper<SqlParameter, SqlDataReader> dbHelper = new DbHelperSqlServer();

 

BaseRepository 的实现类

public bool DeleteByIds(int userId, List<int> ids)
        {
            var whereIds = string.Join(',', ids);
            string sql = "update t_sys_module set p_deleted=@p_deleted,p_lastmodifytime=getdate(),p_modifyuserid=@userid where p_id in (@ids)";
            sql = sql.Replace("@ids", whereIds);
            var sqlParams = new List<SqlParameter>() {
                new SqlParameter("@p_deleted", true),
                new SqlParameter("@userid",userId)
            };
            return dbHelper.ExecuteNonQuery(DataBaseName, sql, sqlParams.ToArray()) > 0;
        }

        /// <summary>
        /// 根据登录用户ID获取树状结构的菜单列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public List<T_sys_moduleModel> GetModulesByUserId(int userId)
        {
            string sql = @"SELECT DISTINCT m.*
                            FROM dbo.t_sys_userroles ur
                                LEFT JOIN dbo.t_sys_rolemodule rm
                                    ON ur.p_roleid = rm.p_roleid
                                LEFT JOIN dbo.t_sys_module m
                                    ON m.p_id = rm.p_moduleid
                            WHERE ur.p_userid = @userid
                                  AND m.p_enabled = 1
                                  AND m.p_deleted = 0";
            var sqlParams = new List<SqlParameter>() {
                new SqlParameter("@userid",userId)
            };
            //执行SQL语句查询 方式1 返回 DataTable 不能返回具体的 Model实体
            //dbHelper.QueryRead<DataTable>(DataBaseName, sql, sqlParams.ToArray());
            //执行SQL语句查询 方式2 可以返回具体的 Model实体
            return SqlExe.ExeGetListBySql<T_sys_moduleModel>(DataBaseName, sql, sqlParams.ToArray());
        }

 

posted @ 2022-10-26 15:33  baivfhpwxf  阅读(65)  评论(0编辑  收藏  举报