using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class image_Detail : System.Web.UI.Page
{
string sqlstr;
DB db = new DB();
DataSet ds = new DataSet();
int PageSize;
int CurrentPage;
int RecordCount;
int PageCount; protected void Page_Load(object sender, EventArgs e)
{
sqlstr = "select * from image";
//Response.Write(Request.QueryString.Get(0).ToString().Trim());
//Label3.Text = Request.QueryString.Get(0).ToString().Trim();
ds = db.GetDataTableBySql(sqlstr);
if (ds.Tables[0].Rows.Count != 0)
{
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataList1.DataBind();
}
ViewState["CurrentPage"] = 0;
PageSize = 3;
sqlstr = "select * from image";
ds = db.GetDataTableBySql(sqlstr);
if (ds.Tables[0].Rows.Count != 0)
{
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataList1.DataBind();
} if (Page.IsPostBack == false)
{
ViewState["PageIndex"] = 0; sqlstr = "select count(*) as count from image";
ds = db.GetDataTableBySql(sqlstr);
if (ds.Tables[0].Rows.Count != 0)
{
RecordCount = int.Parse(ds.Tables[0].Rows[0]["count"].ToString());
}
else
{
RecordCount = 0;
}
if (RecordCount % PageSize == 0)
{
PageCount = RecordCount / PageSize;
}
else
{
PageCount = RecordCount / PageSize + 1;
}
ViewState["PageCount"] = PageCount;
this.Label2.Text = PageCount.ToString(); //显示总页数
BindList(); }
} private void BindList()
{
int StartIndex = CurrentPage * PageSize; //设定导入的起始地址
sqlstr = "select * from image";
SqlConnection con = new SqlConnection();
con.ConnectionString = db.GetConnectionString();
con.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);
da.Fill(ds, StartIndex, PageSize, "image"); this.DataList1.DataSource = ds.Tables["image"].DefaultView;
this.DataList1.DataBind(); PageCount = (int)ViewState["PageCount"];//获得总页数
this.ImageButton1.Enabled = true;
this.ImageButton2.Enabled = true;
if (CurrentPage == (PageCount - 1))
this.ImageButton2.Enabled = false;
if (CurrentPage == 0)
this.ImageButton1.Enabled = false;
this.Label1.Text = Convert.ToString(CurrentPage + 1);
} protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引 CurrentPage = CurrentPage - 1;
ViewState["PageIndex"] = CurrentPage;
BindList();
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引 CurrentPage = CurrentPage + 1;
ViewState["PageIndex"] = CurrentPage;
BindList();
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class image_Detail : System.Web.UI.Page
{
string sqlstr;
DB db = new DB();
DataSet ds = new DataSet();
int PageSize;
int CurrentPage;
int RecordCount;
int PageCount; protected void Page_Load(object sender, EventArgs e)
{
sqlstr = "select * from image";
//Response.Write(Request.QueryString.Get(0).ToString().Trim());
//Label3.Text = Request.QueryString.Get(0).ToString().Trim();
ds = db.GetDataTableBySql(sqlstr);
if (ds.Tables[0].Rows.Count != 0)
{
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataList1.DataBind();
}
ViewState["CurrentPage"] = 0;
PageSize = 3;
sqlstr = "select * from image";
ds = db.GetDataTableBySql(sqlstr);
if (ds.Tables[0].Rows.Count != 0)
{
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataList1.DataBind();
} if (Page.IsPostBack == false)
{
ViewState["PageIndex"] = 0; sqlstr = "select count(*) as count from image";
ds = db.GetDataTableBySql(sqlstr);
if (ds.Tables[0].Rows.Count != 0)
{
RecordCount = int.Parse(ds.Tables[0].Rows[0]["count"].ToString());
}
else
{
RecordCount = 0;
}
if (RecordCount % PageSize == 0)
{
PageCount = RecordCount / PageSize;
}
else
{
PageCount = RecordCount / PageSize + 1;
}
ViewState["PageCount"] = PageCount;
this.Label2.Text = PageCount.ToString(); //显示总页数
BindList(); }
} private void BindList()
{
int StartIndex = CurrentPage * PageSize; //设定导入的起始地址
sqlstr = "select * from image";
SqlConnection con = new SqlConnection();
con.ConnectionString = db.GetConnectionString();
con.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);
da.Fill(ds, StartIndex, PageSize, "image"); this.DataList1.DataSource = ds.Tables["image"].DefaultView;
this.DataList1.DataBind(); PageCount = (int)ViewState["PageCount"];//获得总页数
this.ImageButton1.Enabled = true;
this.ImageButton2.Enabled = true;
if (CurrentPage == (PageCount - 1))
this.ImageButton2.Enabled = false;
if (CurrentPage == 0)
this.ImageButton1.Enabled = false;
this.Label1.Text = Convert.ToString(CurrentPage + 1);
} protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引 CurrentPage = CurrentPage - 1;
ViewState["PageIndex"] = CurrentPage;
BindList();
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引 CurrentPage = CurrentPage + 1;
ViewState["PageIndex"] = CurrentPage;
BindList();
}
}
dt = db.GetDataTableBySql(sqlstr);
if (dt.Rows.Count != 0)
{
...
}
DB类代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;/// <summary>
/// DB 的摘要说明
/// </summary>
public class DB
{
public SqlConnection Con = new SqlConnection();
public SqlCommand Com = new SqlCommand();
public SqlDataAdapter Da = new SqlDataAdapter();
public DataSet Ds = new DataSet();
public DB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public String GetConnectionString()
{
String ConStr;
ConStr = System.Configuration.ConfigurationManager.AppSettings.Get(0).ToString();
return ConStr;
}
public DataSet GetDataTableBySql(String SqlStr)
{
Con.ConnectionString = GetConnectionString();
Com.Connection = Con;
Com.CommandText = SqlStr;
Da.SelectCommand = Com;
try
{
Ds.Clear();
Con.Open();
Da.Fill(Ds);
Con.Close();
}
catch (SqlException)
{
Con.Close();
}
return Ds;
}
public Boolean UpdateDataBySql(String SqlStr)
{
Con.ConnectionString = GetConnectionString();
Com.Connection = Con;
Com.CommandText = SqlStr;
try
{
Con.Open();
Com.ExecuteNonQuery();
Con.Close();
return true;
}
catch (Exception)
{
Con.Close();
return false;
}
}
}
if (ds.Tables.Count==0 || ds.Tables[0].Rows.Count == 0) return;
先判断ds为不为空
然后里面的表 为不为空,
最后判断 表里面的行为不为空
最后 才能放心的用这个里面的数据
如果在程序中 不判断的话 ,一般会有bug
调试运行,先看看这个返回值是否正确