青鸟IT汇的博客

做一名高素质的程序员

mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
using HouseSys.Models;
using System.Data.SqlClient;

namespace HouseSys.DLL
{
    /// <summary>
    /// 房屋的数据访问层
    /// </summary>
    public class HouseDLL
    {
        /// <summary>
        /// 分页查询所有房屋信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<HouseModel> GetHousesAll(int  pageIndex,int pageSize,ConditionModel cond)
        {
            List<HouseModel> houseList = new List<HouseModel>();
            string sql = "select top "+pageSize+" * from house where  HouseId not in (select top "+(pageIndex-1)*pageSize+"  HouseId from House where 1=1) and 1=1 ";
            //动态查询
            if(cond!=null)
            {
                //根据标题
                if (cond.Title != null)
                {
                    sql += " and Title like"+cond.Title;
                }
                
                //最低价格到最高价格
                if (cond.StartPrice != null && cond.EndPrice != null)
                {
                    sql += " and Price >=" + cond.StartPrice + " and Price <= " + cond.EndPrice;
                }
                //根据最低的面积
                if(cond.StartProportion!=null && cond.EndProportion!=null)
                {
                    sql += " and floorage >=" + cond.StartProportion + " and floorage<="+cond.EndProportion;
                }

            }
            using (SqlDataReader reader = SqlHelper.ExcuteReader(sql, CommandType.Text, null))
            {
                while(reader.Read())
                {
                    HouseModel house = new HouseModel();
                    house.Contract = reader["Contract"].ToString();
                    house.Description = reader["Description"].ToString();
                    house.Floorage = Convert.ToDouble(reader["Floorage"]);
                    house.HouseId = Convert.ToInt32(reader["houseid"]);
                    house.Price = Convert.ToDouble(reader["Price"]);
                    house.PublishTime = Convert.ToDateTime(reader["PublishTime"]);
                    house.PublishUser = new UserDLL().GetUserById(Convert.ToInt32(reader["PublishUser"]));
                    house.Street = new StreetDLL().GetStreetById(Convert.ToInt32(reader["streetid"]));
                    house.Title = reader["title"].ToString();
                    house.Type = new HouseTypeDLL().GetHouseTypeById(Convert.ToInt32(reader["typeid"]));
                    houseList.Add(house);
                }
            }
            return houseList;
        }

        /// <summary>
        /// 查询总记录数
        /// </summary>
        /// <returns></returns>
        public int GetHouseCount()
        {
            string sql = "select count(1) from House";
            int rel = Convert.ToInt32(SqlHelper.ExecuteScalar(sql,CommandType.Text,null));
            return rel;
        }
    }
}

阅读更多

扫码向博主提问

去开通我的Chat快问

qq_34137397

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • javaweb
  • 百度云
  • 阿里云
  • sql
  • asp.net
版权声明:文章可以随便转载,但是转载时带上原文地址来源,侵权必究 https://blog.csdn.net/qq_34137397/article/details/74625488
个人分类: ASP MVC .NET
上一篇mvc.net分页查询案例——mvc-paper.css
下一篇mvc.net分页查询案例——实体层(HouseModel.cs)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭