GridView 自带分页

    用三层做的。

一、Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    [Serializable]
    public class StudentInfo
    {
        public int Sid { get; set; }
        public string Sname { get; set; }
        public string Gender { get; set; }
        public byte Age { get; set; }
    }
}

二、DBUtility

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DBUtility
{
    public abstract class SqlHelper
    {
        private readonly static string CONNSTR = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
        private static void PrepareCommand(SqlCommand sqlCmd,CommandType commandType,
            string commandText,params SqlParameter[] sps)
        {
            sqlCmd.CommandText = commandText;
            sqlCmd.CommandType = commandType;
            if (sps != null)
            {
                sqlCmd.Parameters.AddRange(sps);
            }
        }
        public static int ExcuteNonQuery(CommandType commandType,
            string commandText, params SqlParameter[] sps)
        {
            SqlConnection sqlConn = new SqlConnection(CONNSTR);
            SqlCommand sqlCmd = sqlConn.CreateCommand();
            PrepareCommand(sqlCmd,commandType,commandText,sps);
            int rowCount = 0;
            try
            {
                sqlConn.Open();
                rowCount = sqlCmd.ExecuteNonQuery();
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                }
            }
            return rowCount;
        }
        public static SqlDataReader ExcuteReader(CommandType commandType,
            string commandText, params SqlParameter[] sps)
        {
            SqlConnection sqlConn = new SqlConnection(CONNSTR);
            SqlCommand sqlCmd = sqlConn.CreateCommand();
            PrepareCommand(sqlCmd, commandType, commandText, sps);
            SqlDataReader reader = null;
            try
            {
                sqlConn.Open();
                reader = sqlCmd.ExecuteReader();
            }
            catch
            {
                if (sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                }
            }
            return reader;
        }

        public static DataTable Pager(CommandType commandType,
            string commandText, params SqlParameter[] sps)
        {
            SqlConnection sqlConn = new SqlConnection(CONNSTR);
            SqlCommand sqlCmd = sqlConn.CreateCommand();
            PrepareCommand(sqlCmd,commandType,commandText,sps);
            SqlDataAdapter adapter = new SqlDataAdapter(sqlCmd);
            DataTable table = new DataTable();
            try
            {
                adapter.Fill(table);
            }
            catch
            {
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                }
            }
            return table;
        }

        public static byte[] ExecuteScalar(CommandType commandType,
            string commandText, params SqlParameter[] sps)
        {
            SqlConnection sqlConn = new SqlConnection(CONNSTR);
            SqlCommand sqlCmd = sqlConn.CreateCommand();
            PrepareCommand(sqlCmd, commandType, commandText, sps);
            byte[] rowCount;
            try
            {
                sqlConn.Open();
                rowCount =(byte[]) sqlCmd.ExecuteScalar();
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                }
            }
            return rowCount;
        }
    }
}

 

三、BLL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using DBUtility;

namespace BLL
{
    public class Student
    {
        public List<StudentInfo> Student_Selects()
        {
            List<StudentInfo> list = null;
            StudentInfo info = null;
            using (SqlDataReader reader = SqlHelper.ExcuteReader(CommandType.StoredProcedure, "sp_selects", null))
            {
                if (reader != null)
                {
                    list = new List<StudentInfo>();
                    while (reader.Read())
                    {
                        info = new StudentInfo()
                        {
                            Sid=reader.GetInt32(0),
                            Sname=reader.GetString(1),
                            Gender=reader.GetString(2),
                            Age=reader.GetByte(3)
                        };
                        list.Add(info);
                    }
                }
            }
            return list;
        }

    }
}

 

四、shuang/UI层

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="shuangs.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
            AutoGenerateColumns="False" PageSize="2"
            onpageindexchanging="GridView1_PageIndexChanging">
            <Columns>
                <asp:TemplateField HeaderText="编号">
                <ItemTemplate>
                <%# Eval("Sid") %>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Sname" HeaderText="姓名" />
                <asp:BoundField DataField="Gender" HeaderText="性别" />
                <asp:BoundField DataField="Age" HeaderText="年龄" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace shuangs
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private Student stu = new Student();
        protected void Page_Load(object sender, EventArgs e)
        {
            BinGrid();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BinGrid();
        }
        private void BinGrid()
        {
            GridView1.DataSource = stu.Student_Selects();
            GridView1.DataBind();
        }
    }
}

 

五、存储过程

create procedure sp_select
as
select * from student

go

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值