设为首页 收藏本站
查看: 707|回复: 0

[经验分享] 本站原创sql server 2005 分页存储过程(同时得到记录总数)及其运用

[复制链接]

尚未签到

发表于 2016-11-8 08:35:58 | 显示全部楼层 |阅读模式
导读:
  先看看运用:
  using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Web;
  using System.Web.SessionState;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.HtmlControls;
  namespace behind
  {
  /// <summary><br>  /// members 的摘要说明。 <br>  /// </summary>
  public partial class members : System.Web.UI.Page
  {
  protected int allrecord=0;//记录总数
  private void Page_Load(object sender, System.EventArgs e)
  {
  behind.checkLogin.check(Session["administrator"],this,"您未登陆或者登陆超时","top.location='../login.aspx'");
  //获取页码
  string page = Request.QueryString["page"];
  if (!web.webfunction.isnum(page))
  page = "1";
  list.DataSource = web.PagerSql2005.PageData("members", "*", "memberid", 50, int.Parse(page), true, "", out allrecord);
  list.DataBind();
  //分页--这个是自己写的,很久以前的
  web.PagerOutString_en p = new web.PagerOutString_en();
  p.pagesize = 50;
  p.pagenow = int.Parse(page);
  p.querystring = this.Request.QueryString;
  p.size = allrecord
  pagebar.Text = p.showtext;
  p = null;
  }
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
  //
  // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
  //
  InitializeComponent();
  base.OnInit(e);
  }
  
  /// <summary><br>  /// 设计器支持所需的方法 - 不要使用代码编辑器修改 <br>  /// 此方法的内容。 <br>  /// </summary>
  private void InitializeComponent()
  {
  this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
  }
  }
  这是web.PagerSql2005.PageData
  using System;
  using System.Data;
  using System.Data.SqlClient;
  namespace web
  {
  /// <summary><br>  /// PagerSql 的摘要说明 <br>  /// </summary>
  public class PagerSql2005
  {
  public PagerSql2005()
  {
  //
  // TODO: 在此处添加构造函数逻辑
  //
  }
  public static DataTable PageData
  (string tblName,
  string fldCow,
  string fldName,
  int PageSize,
  int PageIndex,
  bool OrderType,
  string strWhere,
  out int count
  )
  {
  web.Database data = new web.Database();
  SqlParameter[] prams = {
  data.MakeInParam("@tblName", SqlDbType.VarChar, 50, tblName),
  data.MakeInParam("@fldCow", SqlDbType.VarChar, 100, fldCow),
  data.MakeInParam("@fldName", SqlDbType.VarChar, 100, fldName),
  data.MakeInParam("@PageSize", SqlDbType.Int, 50, PageSize),
  data.MakeInParam("@PageIndex", SqlDbType.Int, 50, PageIndex),
  data.MakeInParam("@OrderType", SqlDbType.Bit, 1, OrderType==true?1:0),
  data.MakeInParam("@strWhere", SqlDbType.VarChar, 200, strWhere),
  data.MakeOutParam("@count", SqlDbType.Int, 4)
  };
  DataTable dt=data.RunProcToDataTable("_PagerSql2005_out_count", prams);
  data.Close(); data.Dispose();
  count = (int)prams[7].Value;
  return dt;
  }
  }
  }
  以下是sql server 2005 分页存储过程的代码,同时得到记录总数:
  /*
  GO
  -- 对象: StoredProcedure [dbo].[_PagerSql2005_out_count] 脚本日期: 03/31/2007 14:51:11 -
  SET ANSI_NULLS ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  create procedure [dbo].[_PagerSql2005_out_count]
  (
  --1,参数的括号可要可不要,有默认值的参数,在调用的时候,可以不写出来
  --2,调用:
  --declare @i int
  --exec _PagerSql2005_out_count 'list','id,title','id',3,4,1,'classid=6',@i out
  @tblName varchar(100), -- 表名
  @fldCow varchar(100)='*', -- 要查询的列
  @fldName varchar(255), -- 排序列
  @PageSize int = 10, -- 页尺寸
  @PageIndex int = 1, -- 页码
  @OrderType bit = 1, -- 设置排序类型, 1则降序
  @strWhere varchar(200) = '', -- 查询条件 (注意: 不要加 where)
  @count int output --输入符合条件的记录的总数
  )
  AS
  declare @strSQL varchar(1000); -- 主语句
  declare @strOrder varchar(500) ; -- 排序类型
  declare @strTmp varchar(100) ; --临时变量
  declare @endIndex int; -- 结束的索引
  declare @startIndex int; -- 开始的索引
  declare @countSql nvarchar(500); --查询记录总数的SQL
  --得到索引
  set @startIndex=(@PageIndex-1)*@PageSize + 1;--注意,这里要加1
  set @endIndex=@PageIndex*@PageSize;
  --生成排序语句
  --为了多表联合查询,这里要把表名字和排序字段的[]去掉-
  if @OrderType != 0
  set @strOrder = ' order by ' + @fldName + ' desc'
  else
  set @strOrder = ' order by ' + @fldName + ' asc'
  set @strSQL = '(select top ' + ltrim(str(@endIndex)) + ' '+@fldCow+','
  + 'row_number() over ('+ @strOrder +') as rownumber from '
  + @tblName + '' ;
  set @countSql= 'select @count=count('+@fldName+') from '+ @tblName ;
  
  if @strWhere! = ''
  begin
  set @strSQL =@strSQL+ ' where ('+ @strWhere + ') ';
  set @countSql=@countSql + ' where ('+ @strWhere + ') ';
  end
  set @strSQL =@strSQL+ ') as tblTmp'
  --得到记录总数
  set @countSql=N'select @count=count(*) from ' + @tblName;
  if @strWhere! = ''
  set @countSql=@countSql+ N' where ' + @strWhere;
  EXEC sp_executesql @countSql,N'@count int out',@count out
  set @strSQL = 'select * from ' + @strSQL + ' where rownumber between ' + ltrim(str(@startIndex)) + ' and '
  + ltrim(str(@endIndex));
  --执行主语句
  set nocount on -- 防止显示有关受影响的行数的信息
  exec (@strSQL)
  --print @strSQL
  */
  以下是DATABASE代码:
  using System;
  using System.ComponentModel;
  using System.Collections;
  using System.Diagnostics;
  using System.Data;
  using System.Data.SqlClient;
  using System.Configuration;
  namespace web
  {
  public class Database : IDisposable
  {
  private SqlConnection con;
  public Database()
  {
  }
  public System.Data.DataTable RunProcToDataTable(string procName, SqlParameter[] prams)
  {
  //执行带参数的存储过程,返回datatable
  SqlCommand cmd = CreateCommand(procName, prams);
  SqlDataAdapter da = new SqlDataAdapter(cmd);
  System.Data.DataTable dt = new DataTable();
  da.Fill(dt);
  da.Dispose();
  cmd.Dispose();
  this.Close();
  return dt;
  }
  public System.Data.DataTable RunProcToDataTable(string procName)
  {
  //执行不带参数的存储过程,返回datatable
  SqlCommand cmd = CreateCommand(procName, null);
  SqlDataAdapter da = new SqlDataAdapter(cmd);
  System.Data.DataTable dt = new DataTable();
  da.Fill(dt);
  da.Dispose();
  cmd.Dispose();
  this.Close();
  return dt;
  }
  public void RunProc(string procName, SqlParameter[] prams)
  {
  //执行带参数的存储过程
  SqlCommand cmd = CreateCommand(procName, prams);
  cmd.ExecuteNonQuery();
  this.Close();
  return;
  }
  public void RunProc(string procName)
  {
  //执行不带参数的存储过程
  SqlCommand cmd = CreateCommand(procName, null);
  cmd.ExecuteNonQuery();
  this.Close();
  return;
  }
  public void RunProcToReader(string procName, out SqlDataReader dataReader)
  {
  //执行不带参数的存储过程,返回datareader
  SqlCommand cmd = CreateCommand(procName, null);
  dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
  }
  public void RunProcToReader(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
  {
  //执行带参数的存储过程,返回datareader
  SqlCommand cmd = CreateCommand(procName, prams);
  dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
  }
  
  private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
  {
  //构造SqlCommand
  Open();
  SqlCommand cmd = new SqlCommand(procName, con);
  cmd.CommandType = CommandType.StoredProcedure;
  if (prams != null)
  {
  foreach (SqlParameter parameter in prams)
  cmd.Parameters.Add(parameter);
  }
  
  return cmd;
  }
  private void Open()
  {
  //打开数据库连接
  if (con == null)
  {
  con = new SqlConnection(ConfigurationSettings.AppSettings["cnstr"]);
  con.Open();
  return;
  }
  
  if (con.State==ConnectionState.Closed)
  {
  con.Open();
  return;
  }
  }
  public void Close()
  {
  //关闭数据库连接
  if (con != null)
  con.Close();
  }
  public void Dispose()
  {
  //释放资源
  if (con != null)
  {
  con.Dispose();
  con = null;
  }
  }
  public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
  {
  //构造输入参数
  return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
  }
  public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
  {
  //构造输出参数
  return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
  }
  public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
  {
  //构造参数
  SqlParameter param;
  if(Size >0)
  param = new SqlParameter(ParamName, DbType, Size);
  else
  param = new SqlParameter(ParamName, DbType);
  param.Direction = Direction;
  if (!(Direction == ParameterDirection.Output &&Value == null))
  param.Value = Value;
  return param;
  }
  }
  }

本文转自
http://omeweb.com/content.aspx?id=2316

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-297154-1-1.html 上篇帖子: SQL Server 2005中的分区表(四):删除(合并)一个分区 (转) 下篇帖子: archiva server is service_unavailable
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表