也懒得打其他字了,直接进入主题吧
1.首先,数据表Rex_Test
ID | int | 自增 |
tName | varchar(10) | 姓名 |
tEmail | varchar(80) | 邮箱 |
2.至于代码里的Jqurey EasyUI包就直接去官网下载吧。当然其他地方也行,这里就不多做说明了。
3.前台显示页面
Default.aspx
myWeb ID 姓名 邮箱
4. Handler3.ashx(初始绑定)
<%@ WebHandler Language="C#" Class="Handler3" %>using System;using System.Web;using System.Data;using System.Data.SqlClient;using System.Text;using System.IO;public class Handler3 : IHttpHandler { public void ProcessRequest(HttpContext context) { Query(context); } public void Query(HttpContext context) { //调用B层的方法从而获取数据库的Dataset SqlHelp sqla = new SqlHelp(); string strfaca = "select * from Rex_Test"; DataSet ds = sqla.GetDataSet(strfaca); //将Dataset转化为Datable DataTable dt = ds.Tables[0]; int count = dt.Rows.Count; string strJson = Dataset2Json(ds, count);//DataSet数据转化为Json数据 context.Response.Write(strJson);//返回给前台页面 context.Response.End(); } /// /// DataSet转换成Json格式 /// /// DataSet /// public static string Dataset2Json(DataSet ds, int total = -1) { StringBuilder json = new StringBuilder(); foreach (DataTable dt in ds.Tables) { //{"total":5,"rows":[ json.Append("{\"total\":"); if (total == -1) { json.Append(dt.Rows.Count); } else { json.Append(total); } json.Append(",\"rows\":["); json.Append(DataTable2Json(dt)); json.Append("]}"); } return json.ToString(); } /// /// dataTable转换成Json格式 /// /// /// public static string DataTable2Json(DataTable dt) { StringBuilder jsonBuilder = new StringBuilder(); for (int i = 0; i < dt.Rows.Count; i++) { jsonBuilder.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { jsonBuilder.Append("\""); jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append("\":\""); jsonBuilder.Append(dt.Rows[i][j].ToString()); jsonBuilder.Append("\","); } if (dt.Columns.Count > 0) { jsonBuilder.Remove(jsonBuilder.Length - 1, 1); } jsonBuilder.Append("},"); } if (dt.Rows.Count > 0) { jsonBuilder.Remove(jsonBuilder.Length - 1, 1); } return jsonBuilder.ToString(); } public bool IsReusable { get { return false; } }}
5.add.ashx(新增数据)
<%@ WebHandler Language="C#" Class="add" %>using System;using System.Web;public class add : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string result = string.Empty; string name = context.Request.QueryString["name"]; string email = context.Request.QueryString["email"]; SqlHelp sqla = new SqlHelp(); string sqlString = "insert into Rex_Test(tName,tEmail) values('"+name+"','"+email+"')"; bool aa = sqla.ExecuteNonQuery(sqlString); sqla.SqlClose(); if (aa) { result = "加入纪录成功"; } context.Response.Write(result); } public bool IsReusable { get { return false; } }}
6.del.ashx(删除数据)
<%@ WebHandler Language="C#" Class="del" %>using System;using System.Web;public class del : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string result = string.Empty; string id = context.Request.QueryString["id"]; SqlHelp sqla = new SqlHelp(); string sqlString = "delete from Rex_Test where ID='" + id + "'"; bool aa = sqla.ExecuteNonQuery(sqlString); sqla.SqlClose(); if (aa) { result = "删除成功"; } context.Response.Write(result); } public bool IsReusable { get { return false; } }}
7.refresh.ashx(更新数据)
<%@ WebHandler Language="C#" Class="refresh" %>using System;using System.Web;public class refresh : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string result = string.Empty; string id = context.Request.QueryString["id"]; string name = context.Request.QueryString["name"]; string email = context.Request.QueryString["email"]; SqlHelp sqla = new SqlHelp(); string sqlString = "update Rex_Test set tName='"+name+"',tEmail='"+email+"' where ID='" + id + "'"; bool aa = sqla.ExecuteNonQuery(sqlString); sqla.SqlClose(); if (aa) { result = "更新成功"; } context.Response.Write(result); } public bool IsReusable { get { return false; } }}
8.基本上这个样子就差点儿相同了。更加细化的东西还没来得及做,下次弄好了才发吧。
哦。对了,代码里的SqlHelp sqla = new SqlHelp();事实上就是个sql操作类,自己写的,网上有非常多这样的的SqlHelper。这个我就不贴出来了吧。