Gridview分页查询数据,当数据为空时候在查询语句中的判断

今天郁闷了半天,当查询数据列表时候由于数据库没有判断是否为空,已导致查询语句不通过,想了一种方法,希望对你们也有所帮助昂!

用户表名称: SysUser ;给其起了一个别名:u

员工表名称: BasicEmployee: 给其起了一个别名:e

分别按照用户名称,用户类型,是否冻结:查询用户表的数据:

select u.*,e.Name as RealName from

(select * from SysUser where IsDelete=0

and (@UserName is null or UserName=@UserName)

and (@UserType is null or UserType=@UserType)

and (@IsUse is null or IsUse=@IsUse))   u left join BasicEmployee e on u.EmployeeID=e.EmployeeID

这样就可以了如果@UserName 为空值的时候 那么在后台使用的时候就可以给@UserName=null 如果文本框有值也就是说@UserName数据不为空,@UserName=这个文本框的值;

以下是这个例子:

1 在dal 层中:
  /// <summary>
        /// 用字符串拼接的形式,读取分页的数据
        /// </summary>
        /// <param name="pageSize">分页大小</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="rowCount">数据条数</param>
        /// <param name="orderField">排序字段</param>
        /// <param name="prms">条件字典表</param>
        /// <returns></returns>
        public DataSet GetPaging(int pageSize, int pageIndex, ref int rowCount, string orderField, Dictionary<string, object> prms)
        {
            DB dbUtil = DB.GetInstance();
            StringBuilder sb = new StringBuilder();
            sb.Append("select u.*,e.Name as RealName from (select * from SysUser where IsDelete=0 and (@UserName is null or UserName=@UserName) and (@UserType is null or UserType=@UserType) and (@IsUse is null or IsUse=@IsUse))");
            sb.Append("  u left join BasicEmployee e on u.EmployeeID=e.EmployeeID");
            Database db = this.CreateDatabase();

            //获取数据条数
            DbCommand cmdCount = dbUtil.GetDbCommandDataCount(db, sb.ToString());
            db.AddInParameter(cmdCount, "@UserName", DbType.String, prms["@UserName"]);
            db.AddInParameter(cmdCount, "@UserType", DbType.Int32, prms["@UserType"]);
            db.AddInParameter(cmdCount, "@IsUse", DbType.Int32, prms["@IsUse"]);
            rowCount = Convert.ToInt32(db.ExecuteScalar(cmdCount));
            //获取分页数据
            DbCommand cmdPaging = dbUtil.GetDbCommandPaging(db, sb.ToString(), pageSize, pageIndex, rowCount, orderField);
            db.AddInParameter(cmdPaging, "@UserName", DbType.String, prms["@UserName"]);
            db.AddInParameter(cmdPaging, "@UserType", DbType.Int32, prms["@UserType"]);
            db.AddInParameter(cmdPaging, "@IsUse", DbType.Int32, prms["@IsUse"]);
            //执行命令获取数据
            return db.ExecuteDataSet(cmdPaging);
        }

BLL中:

 /// <summary>
        /// 用字符串拼接的形式,读取分页的数据
        /// </summary>
        /// <param name="pageSize">分页大小</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="rowCount">数据条数</param>
        /// <param name="orderField">排序字段</param>
        /// <param name="prms">条件字典表</param>
        public DataSet GetPaging(int pageSize, int pageIndex, ref int rowCount, string orderField, Dictionary<string, object> prms)
        {
            return dal.GetPaging(pageSize, pageIndex, ref rowCount, orderField, prms);
        }
前台调用字典表数据:

   public Dictionary<string, object> GetPagingUser()
        {
            dic.Clear();
            string realname = this.txtRealName.Text.Trim();
            string usertype = this.ddlUserType.SelectedValue;
            string isuser=this.rblIsUse.SelectedValue ;
            if (this.txtUserName.Text.Trim().Length == 0)
            {
                dic.Add("@UserName", null);
            }
            else
            {
                dic.Add("@UserName", this.txtUserName.Text.Trim());
            }
            if (usertype =="-1")
            {
                dic.Add("@UserType", null);
            }
            else
            {
                dic.Add("@UserType", usertype);
            }
            if(isuser=="-1")
            {
                dic.Add("@IsUse", null);
            }
                else
            {
                dic.Add("@IsUse", isuser);
            }
            return dic; 
        }

 

 

 

你可能感兴趣的:(GridView)