我使用的是html+一般处理程序。
本次使用的数据库脚本代码如下:
create database DataGrildDB
on(
name='DataGrildDB',
filename='F:\\MyDB\DataGrildDB.mdf'
)
go
use DataGrildDB
go
create table Goods
(
id int primary key identity(1,1),
goodName nvarchar(50) not null,
addTime datetime,
)
go
go
--一次插入三万条数据
declare @i int
set @i=0
while @i<30000
begin
insert into Goods values('农夫山泉'+convert(varchar(50),(@i+1)),getdate())
set @i=@i+1
end
--实现真分页公式
--select top 行 * from Goods where id not in(select top (页-1)* 行 id from Goods)
本次使用的DBHelper类,代码如下:
public class DBHelper
{
private static string DBConStr = "server=.;uid=sa;pwd=123456;database=DataGrildDB";
private static SqlConnection conn;
private static SqlDataAdapter da;
private static SqlCommand cmd;
private static DBHelper dbHelper;
public DBHelper() {
conn = new SqlConnection(DBConStr);
}
public static DBHelper Instance()
{
if (dbHelper==null)
{
dbHelper = new DBHelper();
}
return dbHelper;
}
void DBOpen()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
}
void DBClose()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
public DataTable GetTableBySql(string sql)
{
DBOpen();
DataTable dt = new DataTable();
da = new SqlDataAdapter(sql,conn);
try
{
da.Fill(dt);
return dt;
}
catch (Exception)
{
return null;
}
finally
{
DBClose();
}
}
public bool Execute(string sql)
{
DBOpen();
cmd = new SqlCommand(sql,conn);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch (Exception)
{
return false;
}
finally
{
DBClose();
}
}
public object GetCount(string sql)
{
DBOpen();
cmd = new SqlCommand(sql, conn);
try
{
return cmd.ExecuteScalar();
}
catch (Exception)
{
return 0;
}
finally
{
DBClose();
}
}
}
在开始之前,我们先创建一个静态页面,代码如下:
html页面jquery代码:
一: 查询一般处理程序后台代码如下:
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int page =Convert.ToInt32(context.Request["page"]);
int rows = Convert.ToInt32(context.Request["rows"]);
string name = context.Request["name"];
if (string.IsNullOrWhiteSpace(name))
{
context.Response.Write("{");
context.Response.Write(string.Format("\"total\":{0},\"rows\":{1}", GetCount(), JsonStr(rows, page)));
context.Response.Write("}");
}
else
{
context.Response.Write("{");
context.Response.Write(string.Format("\"total\":{0},\"rows\":{1}", GetCount(name), JsonStr(rows, page,name)));
context.Response.Write("}");
}
}
public bool IsReusable
{
get
{
return false;
}
}
public string JsonStr(int rows,int page)
{
string sql = string.Format(@"select top {0} id,goodName,CONVERT(nvarchar ,addTime,23) addTime from Goods
where id not in (select top (({1}-1)*{2}) id from Goods)", rows, page, rows);
DataTable dt = DBHelper.Instance().GetTableBySql(sql);
return JsonConvert.SerializeObject(dt);
}
public string JsonStr(int rows, int page,string name)
{
string sql = string.Format(@"select top {0} id,goodName,CONVERT(nvarchar ,addTime,23) addTime from Goods
where id not in (select top (({1}-1)*{2}) id from Goods) and goodName like '%{3}%' or id like '%{4}%'" , rows, page, rows,name,name);
DataTable dt = DBHelper.Instance().GetTableBySql(sql);
return JsonConvert.SerializeObject(dt);
}
public int GetCount()
{
string sql = string.Format("select count(id) from Goods");
return Convert.ToInt32( DBHelper.Instance().GetCount(sql));
}
public int GetCount(string name)
{
string sql = string.Format("select count(id) from Goods where id like '%{0}%' or goodName like '%{1}%' ",name,name);
return Convert.ToInt32(DBHelper.Instance().GetCount(sql));
}
}
二、 删除一般处理程序后台代码如下:
public class Handler2 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string ids = context.Request["ids"];
string sql = string.Format("delete from Goods where id in({0})", ids);
if (DBHelper.Instance().Execute(sql))
{
context.Response.Write(1);
}
else
{
context.Response.Write(0);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
三、修改 一般处理程序后台代码如下:
public class Handler3 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string name = context.Request["name"];
int id =int.Parse( context.Request["id"]);
string sql = string.Format("update Goods set goodName='{0}' where id='{1}'",name,id);
if(DBHelper.Instance().Execute(sql))
{
context.Response.Write(1);
}
else
{
context.Response.Write(0);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
四、添加 一般处理程序后台代码如下:
public class addHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string name=context.Request["name"];
string sql = string.Format("insert into Goods values('{0}',getdate())",name);
if (DBHelper.Instance().Execute(sql))
{
context.Response.Write(1);
}
else
{
context.Response.Write(0);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}