asp.net pager控件7.0 使用方法

asp.net pager7.0控件的使用方法较以前有较大改变 。

在这里记录一下以备以后查询。

1:用到了SQLHelper类

  1using System;
  2using System.Data;
  3using System.Data.SqlClient;
  4using System.Configuration;
  5using System.Web;
  6using System.Web.Security;
  7using System.Web.UI;
  8using System.Web.UI.WebControls;
  9using System.Web.UI.WebControls.WebParts;
 10using System.Web.UI.HtmlControls;
 11
 12/// <summary>
 13/// The SqlHelper class is intended to encapsulate high performance, 
 14/// scalable best practices for common uses of SqlClient.
 15/// </summary>

 16public sealed class SqlHelper
 17{
 18    private SqlHelper() { }
 19
 20    //Database connection strings
 21    public static readonly string CONN_STRING = ConfigurationManager.ConnectionStrings["SnipEngineConnection"].ConnectionString;
 22
 23    /// <summary>
 24    /// Execute a SqlCommand that returns a resultset against the database specified in the connection string 
 25    /// using the provided parameters.
 26    /// </summary>
 27    /// <remarks>
 28    /// e.g.:  
 29    ///  SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
 30    /// </remarks>
 31    /// <param name="cmdType">the CommandType (stored procedure, text, etc.)</param>
 32    /// <param name="cmdText">the stored procedure name or T-SQL command</param>
 33    /// <param name="cmdParms">an array of SqlParamters used to execute the command</param>
 34    /// <returns>A SqlDataReader containing the results</returns>

 35    public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
 36    {
 37        SqlCommand cmd = new SqlCommand();
 38        SqlConnection conn = new SqlConnection(CONN_STRING);
 39
 40        // we use a try/catch here because if the method throws an exception we want to 
 41        // close the connection throw code, because no datareader will exist, hence the 
 42        // commandBehaviour.CloseConnection will not work
 43        try
 44        {
 45            PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
 46            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
 47            cmd.Parameters.Clear();
 48            return rdr;
 49        }

 50        catch
 51        {
 52            conn.Close();
 53            throw;
 54        }

 55    }

 56
 57    /// <summary>
 58    /// Execute a SqlCommand that returns the first column of the first record against the database specified in the connection string 
 59    /// using the provided parameters.
 60    /// </summary>
 61    /// <remarks>
 62    /// e.g.:  
 63    ///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
 64    /// </remarks>
 65    /// <param name="cmdType">the CommandType (stored procedure, text, etc.)</param>
 66    /// <param name="cmdText">the stored procedure name or T-SQL command</param>
 67    /// <param name="cmdParms">an array of SqlParamters used to execute the command</param>
 68    /// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>

 69    public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
 70    {
 71        SqlCommand cmd = new SqlCommand();
 72
 73        using (SqlConnection conn = new SqlConnection(CONN_STRING))
 74        {
 75            PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
 76            object val = cmd.ExecuteScalar();
 77            cmd.Parameters.Clear();
 78            return val;
 79        }

 80    }

 81
 82    /// <summary>
 83    /// Execute a SqlCommand that returns the first column of the first record against an existing database connection 
 84    /// using the provided parameters.
 85    /// </summary>
 86    /// <remarks>
 87    /// e.g.:  
 88    ///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
 89    /// </remarks>
 90    /// <param name="conn">an existing database connection</param>
 91    /// <param name="cmdType">the CommandType (stored procedure, text, etc.)</param>
 92    /// <param name="cmdText">the stored procedure name or T-SQL command</param>
 93    /// <param name="cmdParms">an array of SqlParamters used to execute the command</param>
 94    /// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>

 95    public static object ExecuteScalar(SqlConnection conn, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
 96    {
 97
 98        SqlCommand cmd = new SqlCommand();
 99
100        PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
101        object val = cmd.ExecuteScalar();
102        cmd.Parameters.Clear();
103        return val;
104    }

105
106
107    /// <summary>
108    /// Prepare a command for execution
109    /// </summary>
110    /// <param name="cmd">SqlCommand object</param>
111    /// <param name="conn">SqlConnection object</param>
112    /// <param name="trans">SqlTransaction object</param>
113    /// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
114    /// <param name="cmdText">Command text, e.g. Select * from Products</param>
115    /// <param name="cmdParms">SqlParameters to use in the command</param>

116    private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
117    {
118
119        if (conn.State != ConnectionState.Open)
120            conn.Open();
121
122        cmd.Connection = conn;
123        cmd.CommandText = cmdText;
124
125        if (trans != null)
126            cmd.Transaction = trans;
127
128        cmd.CommandType = cmdType;
129
130        if (cmdParms != null)
131        {
132            foreach (SqlParameter parm in cmdParms)
133                cmd.Parameters.Add(parm);
134        }

135    }

136}

137

2:网页文件中的代码如下:

 1 protected void Page_Load(object sender, EventArgs e)
 2    {
 3        if (!IsPostBack)
 4        {
 5            if (Request.QueryString["no"!= null && Request.QueryString["no"].ToString() != "")
 6            {
 7                int totalOrders = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "getArticleCount",
 8                      new SqlParameter("@pno"int.Parse(Request.QueryString["no"])));
 9                AspNetPager1.RecordCount = totalOrders;
10                bindData();
11            }

12
13        }

14
15    }

16
17    void bindData()
18    {
19        DataList1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "getArticleContent",
20            new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
21            new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex),
22            new SqlParameter ("@pno",int.Parse (Request .QueryString ["no"])));
23        DataList1.DataBind();
24    }

25    protected void AspNetPager1_PageChanged(object src, EventArgs e)
26    {
27        bindData();
28    }


用到了两个存储过程:

(1)获取记录总数
1alter procedure  getArticleCount
2@pno int
3AS
4    select count(*) from lunwen where pno=@pno;
5    RETURN

(2)返回记录内容
 1CREATE procedure getArticleContent
 2(@startIndex int,
 3@endIndex int,
 4@pno int
 5)
 6as
 7set nocount on
 8declare @indextable table(id int identity(1,1),nid int)
 9set rowcount @endIndex
10insert into @indextable(nid) select id from lunwen where  pno=@pno order by id desc
11
12
13
14select O.id,O.title,O.zuozhe,O.danwei,O.hit,O.pno,O.addtime
15from lunwen O
16inner join @indextable t on 
17O.id=t.nid
18where t.id between @startIndex and @endIndex and pno=@pno order by t.id
19set nocount off
20    RETURN
21
22

 

总结完毕

posted on 2008-01-16 08:22  蓝蓝的天2016  阅读(440)  评论(0)    收藏  举报