前臺(tái)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
后臺(tái)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
GVBind();
}
}
public void GVBind()
{
SqlParameter p=null;
DataSet ds = SqlHelper.ExecuteDataSet(System.Data.CommandType.Text, "select * from D_Information", p);
GridViewItems.DataSource = datalistbind(ds);
GridViewItems.DataBind();
}
private PagedDataSource datalistbind(DataSet dst)
{
PagedDataSource objpage = new PagedDataSource();
objpage.DataSource = dst.Tables[0].DefaultView;
objpage.AllowPaging = true;
//dg1.PageSize = 20;
objpage.PageSize = 20;
int CurPage;
//判斷是否有分??
///
if (Request["iPage"] != null)
{
CurPage = Convert.ToInt32(Request["iPage"]);
}
else
{
CurPage = 1;
}
//?當(dāng)前?
objpage.CurrentPageIndex = CurPage - 1;
Label1.Text = "當(dāng)前?第 + CurPage.ToString() + "/" + objpage.PageCount + ";
Label2.Text = "共有 + objpage.DataSourceCount + "條? + objpage.PageSize + "/;
//判斷不是第一?時(shí)候
if (!objpage.IsFirstPage)
{
this.prev.NavigateUrl = Request.CurrentExecutionFilePath + "?iPage=" + (Convert.ToInt32(CurPage) - 1) + "&Flag=" + Request["Flag"];
}
this.start.NavigateUrl = Request.CurrentExecutionFilePath + "?iPage=1" + "&Flag=" + Request["Flag"];
//判斷不是最后一?時(shí)候
if (!objpage.IsLastPage)
{
this.next.NavigateUrl = Request.CurrentExecutionFilePath + "?ipage=" + (Convert.ToInt32(CurPage) + 1) + "&Flag=" + Request["Flag"];
}
this.max.NavigateUrl = Request.CurrentExecutionFilePath + "?iPage=" + objpage.PageCount + "&Flag=" + Request["Flag"];
if (!IsPostBack)
{
for (int i = 1; i < objpage.PageCount + 1; i++)
{
this.iPage.Items.Add(new ListItem(i.ToString() + ", i.ToString()));
}
}
return objpage;
}
protected void sic(object sender, EventArgs e)
{
SqlParameter p = null;
DataSet ds = SqlHelper.ExecuteDataSet(System.Data.CommandType.Text, "select * from D_Information", p);
PagedDataSource objpage = new PagedDataSource();
objpage.DataSource = ds.Tables[0].DefaultView;
objpage.AllowPaging = true;
//dg1.PageSize = 20;
objpage.PageSize = 20;
int CurPage;
//判斷是否有分?? ///
if (iPage.SelectedValue != null)
{
CurPage = Convert.ToInt32(iPage.SelectedValue);
}
else
{
CurPage = 1;
}
//?當(dāng)前?
objpage.CurrentPageIndex = CurPage - 1;
Label1.Text = "當(dāng)前?第 + CurPage.ToString() + "/" + objpage.PageCount + ";
Label2.Text = "共有 + objpage.DataSourceCount + "條? + objpage.PageSize + "/;
//判斷不是第一?時(shí)候
if (!objpage.IsFirstPage)
{
this.prev.NavigateUrl = Request.CurrentExecutionFilePath + "?iPage=" + (Convert.ToInt32(CurPage) - 1) + "&Flag=" + Request["Flag"];
}
this.start.NavigateUrl = Request.CurrentExecutionFilePath + "?iPage=1" + "&Flag=" + Request["Flag"];
//判斷不是最后一?時(shí)候
if (!objpage.IsLastPage)
{
this.next.NavigateUrl = Request.CurrentExecutionFilePath + "?ipage=" + (Convert.ToInt32(CurPage) + 1) + "&Flag=" + Request["Flag"];
}
this.max.NavigateUrl = Request.CurrentExecutionFilePath + "?iPage=" + objpage.PageCount + "&Flag=" + Request["Flag"];
if (!IsPostBack)
{
for (int i = 1; i < objpage.PageCount + 1; i++)
{
this.iPage.Items.Add(new ListItem(i.ToString() + ", i.ToString()));
}
}
this.GridViewItems.DataSource = objpage; //ds.Tables["dstable"].DefaultView;
this.GridViewItems.DataBind();
}
protected void GridViewItems_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == -1) return;
e.Row.Attributes.Add("bgcolor", "#EFFFDA");
e.Row.Attributes.Add("style", "cursor:hand");
e.Row.Attributes.Add("onMouseOver=this.style.backgroundColor", "#EDEBEF");
e.Row.Attributes.Add("onMouseOut=this.style.backgroundColor", "#F6F6F6");
}
}
sqlhelp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public abstract class SqlHelper
{
public static readonly string connectionString = ConfigurationManager.ConnectionStrings["Devin:SqlServer"].ConnectionString;
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
protected SqlHelper()
{
}
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
}
public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
return ExecuteDataSet(cmdType, cmdText, "DefaultTable", commandParameters);
}
public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, string TableName, params SqlParameter[] commandParameters)
{
DataSet set2;
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, TableName);
adapter.Dispose();
cmd.Dispose();
conn.Close();
set2 = dataSet;
}
catch
{
throw;
}
return set2;
}
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int num = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.Dispose();
connection.Close();
return num;
}
}
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int num = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.Dispose();
return num;
}
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int num = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.Dispose();
return num;
}
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlDataReader reader2;
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
reader2 = reader;
}
catch
{
conn.Close();
throw;
}
return reader2;
}
public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object obj2 = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return obj2;
}
}
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object obj2 = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return obj2;
}
public static SqlParameter[] GetCachedParameters(string cacheKey)
{
SqlParameter[] parameterArray = (SqlParameter[])parmCache[cacheKey];
if (parameterArray == null)
{
return null;
}
SqlParameter[] parameterArray2 = new SqlParameter[parameterArray.Length];
int index = 0;
int length = parameterArray.Length;
while (index < length)
{
parameterArray2[index] = (SqlParameter)((ICloneable)parameterArray[index]).Clone();
index++;
}
return parameterArray2;
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
{
cmd.Transaction = trans;
}
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parameter in cmdParms)
{
if (parameter != null && !string.IsNullOrEmpty((parameter.Value as string)) || (parameter.Value is int))
{
cmd.Parameters.Add(parameter);
}
}
}
}
}