关闭

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

标签: namespace分页C#框架数据
233人阅读 评论(0) 收藏 举报
分类:
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;
        }
    }
}

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:171611次
    • 积分:5372
    • 等级:
    • 排名:第5691名
    • 原创:369篇
    • 转载:34篇
    • 译文:0篇
    • 评论:96条
    个人信息
    微信公众号
    博客专栏