一:在web.config中加入数据库连接字符串:
<?xml version="1.0"?>
<configuration>
<system.web>
</system.web>
<connectionStrings>
<add name="sql" connectionString="Data Source=.;Initial Catalog=MyBookShop;User Id=sa;Password=000000;"/>
</connectionStrings>
</configuration>
二:ListAllUser.html页面:
<body>
<form >
<table>
<tr>
<th>地址</th>
<th>用户名</th>
<th>电话</th>
<th>姓名</th>
<th>Emial</th>
<th></th>
<th></th>
<th></th>
</tr>
@tBody
</table>
</form>
</body>
三:public class ListAllUsers : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";//此处注意
StringBuilder sb = new StringBuilder();
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT Address,LoginId,Phone,Name,Email FROM Users ",connection))
{
System.Data.DataTable dt = new DataTable();
adapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td><a href='Delete.ashx?id={1}'>删除</a></td><td><a href='Detail.ashx?id={1}'>详细</a></td><td><a href='Edit.ashx?id={1}'>Edit</a></td></tr>", row["Address"], row["LoginId"], row["Phone"], row["Name"], row["Email"]);
}
}
string tempStr = System.IO.File.ReadAllText(context.Request.MapPath("ListAllUsers.htm"));
context.Response.Write(tempStr.Replace("@tBody",sb.ToString()));
}
public bool IsReusable {
get {
return false;
}
}
}
四:Delete.ashx
public class Delete : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string id = context.Request["id"]?? "0";//此时的id为前面页面选择某行后传过来的id值
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
using (SqlConnection conn =new SqlConnection(connection))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM Users WHERE LoginId=@LoginId";
cmd.Parameters.Add(new SqlParameter("@LoginId",id));
conn.Open();
cmd.ExecuteNonQuery();
}
}
context.Response.Redirect("ListAllUsers.ashx");
}
public bool IsReusable {
get {
return false;
}
}
}
五 :Detail.html
<body>
<table>
<tr>
<td>用户ID</td>
<td>@ID</td>
</tr>
<tr>
<td>用户名</td>
<td>@LoginId</td>
</tr>
<tr>
<td>姓名</td>
<td>@Name</td>
</tr>
<tr>
<td>电话</td>
<td>@Phone</td>
</tr>
<tr>
<td>地址</td>
<td>@Address</td>
</tr>
<tr>
<td>Email</td>
<td>@Email</td>
</tr>
</table>
</body>
六 :Detail.ashx
public class Detail : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string id = context.Request["id"] ?? "0";
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connection))
{
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT ID,LoginId,Name,Phone,Address,Email FROM Users WHERE LoginId=@LoginId",conn))
{
adapter.SelectCommand.Parameters.Add(new SqlParameter("@LoginId",id));
DataTable dt = new DataTable();
adapter.Fill(dt);
string tempStr = File.ReadAllText(context.Request.MapPath("Detail.htm"));
tempStr = tempStr.Replace("@ID", dt.Rows[0]["ID"].ToString());
tempStr = tempStr.Replace("@LoginId",dt.Rows[0]["LoginId"].ToString());
tempStr = tempStr.Replace("@Name", dt.Rows[0]["Name"].ToString());
tempStr = tempStr.Replace("@Phone", dt.Rows[0]["Phone"].ToString());
tempStr = tempStr.Replace("@Address", dt.Rows[0]["Address"].ToString());
tempStr = tempStr.Replace("@Email", dt.Rows[0]["Email"].ToString());
context.Response.Write(tempStr);
}
}
}
七 :Edit.ashx
<form action="EditProcess.ashx" method="post">
<table>
<tr>
<td>用户ID</td>
<td><input type="text" name="ID" value="@ID"/></td>
</tr>
<tr>
<td>用户名</td>
<td><input type="text" name="LoginId" value="@LoginId"/></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="Name" value="@Name"/></td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="Phone" value="@Phone"/></td>
</tr>
<tr>
<td>地址</td>
<td><input type="text" name="Address" value="@Address"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="Email" value="@Email"/></td>
</tr>
<tr>
<td><input type="submit" value="确定"/><input type="reset" value="取消"/></td>
</tr>
</table>
</form>
八 :Edit.ashx
public class Edit : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string id = context.Request["id"] ?? "0";
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connection))
{
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT ID,LoginId,Name,Phone,Address,Email FROM Users WHERE LoginId=@LoginId", connection))
{
adapter.SelectCommand.Parameters.Add(new SqlParameter("LoginId",id));
DataTable dt = new DataTable();
adapter.Fill(dt);
string strTemp = File.ReadAllText(context.Request.MapPath("Edit.htm"));
strTemp = strTemp.Replace("@ID",dt.Rows[0]["ID"].ToString());
strTemp = strTemp.Replace("@LoginId", dt.Rows[0]["LoginId"].ToString());
strTemp = strTemp.Replace("@Name", dt.Rows[0]["Name"].ToString());
strTemp = strTemp.Replace("@Phone", dt.Rows[0]["Phone"].ToString());
strTemp = strTemp.Replace("@Address", dt.Rows[0]["Address"].ToString());
strTemp = strTemp.Replace("@Email", dt.Rows[0]["Email"].ToString());
context.Response.Write(strTemp);
}
}
}
九 :EditProcess.aspx
public class DeleteProcess : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//int Id = int.Parse(context.Request["ID"]);
string loginId=context.Request["LoginId"];
string name=context.Request["Name"];
string phone=context.Request["Phone"];
string address=context.Request["Address"];
string email=context.Request["Email"];
string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
using (SqlConnection connection = new SqlConnection(strConn))
{
using(SqlCommand cmd=new SqlCommand())
{
cmd.Connection = connection;
connection.Open();
cmd.CommandText = "UPDATE Users SET LoginId=@LoginId,Name=@Name,Phone=@Phone,Address=@Address,Email=@Email";
//cmd.Parameters.Add(new SqlParameter("@ID",Id));
cmd.Parameters.Add(new SqlParameter("@LoginId",loginId));
cmd.Parameters.Add(new SqlParameter("@Name", name));
cmd.Parameters.Add(new SqlParameter("@Phone", phone));
cmd.Parameters.Add(new SqlParameter("@Address", address));
cmd.Parameters.Add(new SqlParameter("@Email", email));
cmd.ExecuteNonQuery();
context.Response.Redirect("ListAllUsers.ashx");
}
}
}