求一个ASP.NET(C#)(存储过程)的分页代码?
想要一个.NET的手工分页代码要求1,不用控件2,最好结合存储过程(sql2000)3,效果和GOOGLE搜索的差不多弄了几天都弄不出来希望高手帮忙最好给一个完整的例子先...
想要一个.NET的手工分页代码
要求 1,不用控件
2,最好结合存储过程(sql2000)
3,效果和GOOGLE搜索的差不多
弄了几天都弄不出来 希望高手帮忙 最好给一个完整的例子
先谢过了 或者 发我邮箱也行!! melack@163.com
采用后再追加分 展开
要求 1,不用控件
2,最好结合存储过程(sql2000)
3,效果和GOOGLE搜索的差不多
弄了几天都弄不出来 希望高手帮忙 最好给一个完整的例子
先谢过了 或者 发我邮箱也行!! melack@163.com
采用后再追加分 展开
2个回答
展开全部
using System;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
const int Record_per_Page = 2;
int PageCount;
int RecCount;
int Page;
protected void Page_Load(object sender, EventArgs e)
{
PageDisPlay();
}
public void PageDisPlay()
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
try
{
conn.ConnectionString = "server=(local);Initial Catalog=classmate;User Id=sa;Password=sa";
cmd.CommandText = "select *from contact ";
cmd.Connection = conn;
da.SelectCommand = cmd;
da.Fill(ds,"contact");
dt = ds.Tables[0];
}
catch(Exception e)
{
Response.Write(e.Message);
}
RecCount = dt.Rows.Count;
StringBuilder strHtml = new StringBuilder("");
if (RecCount > 0)
{
PageCount = RecCount / Record_per_Page;
if (RecCount % Record_per_Page > 0)
{
PageCount++;
}
Page = Convert.ToInt32(Request.QueryString["page"]);
if (Page < 0) Page = 1;
if (Page > PageCount) Page = PageCount;
/* for (int i = 1; i <=PageCount; i++)
{
strHtml.Append("<a href='Default.aspx?page=" + i + "'>");
if (i == Page) strHtml.Append("<b>" + i + "</b>");
else strHtml.Append(i);
strHtml.Append("</a>");
}
Response.Write(strHtml);*/
//strHtml.Remove(0, strHtml.Length);
int start=Record_per_Page*(Page-1);
int end = start + Record_per_Page - 1;
if (end > RecCount - 1) end = RecCount - 1;
Response.Write("<table border='1'><tr>");
Response.Write("<td>id</td><td>name</td><td>address</td><td>phone</td><td>email</td></tr>");
strHtml.Remove(0, strHtml.Length);
for (int i = start; i <=end; i++)
{
strHtml.Append("<tr>");
for (int j = 0;j < 5;j++)
{
strHtml.Append("<td>"+dt.Rows[i][j].ToString()+"</td>");
}
strHtml.Append("</tr>");
}
strHtml.Append("</table>");
Response.Write(strHtml);
strHtml.Remove(0,strHtml.Length);
for (int i = 1; i <= PageCount; i++)
{
strHtml.Append("<a href='分页1.aspx?page=" + i + "'>");
if (i == Page) strHtml.Append("<b>" + i + "</b>");
else strHtml.Append(i);
strHtml.Append("</a>");
}
Response.Write(strHtml);
}
conn.Close();
}
# region //设计器生成的代码
protected override void OnInit(EventArgs e)
{
InitializecComponent();
base.OnInit(e);
}
private void InitializecComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
一个完整的分页程序,拷过去就能用,但没用到存储过程,因为不好写代码
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
const int Record_per_Page = 2;
int PageCount;
int RecCount;
int Page;
protected void Page_Load(object sender, EventArgs e)
{
PageDisPlay();
}
public void PageDisPlay()
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
try
{
conn.ConnectionString = "server=(local);Initial Catalog=classmate;User Id=sa;Password=sa";
cmd.CommandText = "select *from contact ";
cmd.Connection = conn;
da.SelectCommand = cmd;
da.Fill(ds,"contact");
dt = ds.Tables[0];
}
catch(Exception e)
{
Response.Write(e.Message);
}
RecCount = dt.Rows.Count;
StringBuilder strHtml = new StringBuilder("");
if (RecCount > 0)
{
PageCount = RecCount / Record_per_Page;
if (RecCount % Record_per_Page > 0)
{
PageCount++;
}
Page = Convert.ToInt32(Request.QueryString["page"]);
if (Page < 0) Page = 1;
if (Page > PageCount) Page = PageCount;
/* for (int i = 1; i <=PageCount; i++)
{
strHtml.Append("<a href='Default.aspx?page=" + i + "'>");
if (i == Page) strHtml.Append("<b>" + i + "</b>");
else strHtml.Append(i);
strHtml.Append("</a>");
}
Response.Write(strHtml);*/
//strHtml.Remove(0, strHtml.Length);
int start=Record_per_Page*(Page-1);
int end = start + Record_per_Page - 1;
if (end > RecCount - 1) end = RecCount - 1;
Response.Write("<table border='1'><tr>");
Response.Write("<td>id</td><td>name</td><td>address</td><td>phone</td><td>email</td></tr>");
strHtml.Remove(0, strHtml.Length);
for (int i = start; i <=end; i++)
{
strHtml.Append("<tr>");
for (int j = 0;j < 5;j++)
{
strHtml.Append("<td>"+dt.Rows[i][j].ToString()+"</td>");
}
strHtml.Append("</tr>");
}
strHtml.Append("</table>");
Response.Write(strHtml);
strHtml.Remove(0,strHtml.Length);
for (int i = 1; i <= PageCount; i++)
{
strHtml.Append("<a href='分页1.aspx?page=" + i + "'>");
if (i == Page) strHtml.Append("<b>" + i + "</b>");
else strHtml.Append(i);
strHtml.Append("</a>");
}
Response.Write(strHtml);
}
conn.Close();
}
# region //设计器生成的代码
protected override void OnInit(EventArgs e)
{
InitializecComponent();
base.OnInit(e);
}
private void InitializecComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
一个完整的分页程序,拷过去就能用,但没用到存储过程,因为不好写代码
展开全部
SQL server存储:
======================================
CREATE PROCEDURE AddNew
@content text
AS insert into test values (@content)
GO
======================================
CS调用代码:
======================================
string sqlString = "AddNew";
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["sqlConnection"]);
SqlCommand cmd = new SqlCommand(sqlString,conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter();
param = cmd.Parameters.Add("@content",SqlDbType.Text);
param.Direction = ParameterDirection.Input;
param.Value= "zzzzzzzzzzzzzzz"; conn.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close(); conn.Dispose();===================================
======================================
CREATE PROCEDURE AddNew
@content text
AS insert into test values (@content)
GO
======================================
CS调用代码:
======================================
string sqlString = "AddNew";
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["sqlConnection"]);
SqlCommand cmd = new SqlCommand(sqlString,conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter();
param = cmd.Parameters.Add("@content",SqlDbType.Text);
param.Direction = ParameterDirection.Input;
param.Value= "zzzzzzzzzzzzzzz"; conn.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close(); conn.Dispose();===================================
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询