首页 > 代码库 > ASP.NET 一般处理程序删除数据

ASP.NET 一般处理程序删除数据

del.ashx 删除页面

<%@ WebHandler Language="C#" Class="del" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;

public class del : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
int id;
if (int.TryParse(context.Request.QueryString["id"], out id))
{
using (SqlConnection conn = new SqlConnection(GetConnStr()))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM Employee Where EmpId=@id";
cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters["@id"].Value = http://www.mamicode.com/id;
conn.Open();
if (cmd.ExecuteNonQuery() > 0)
{
context.Response.Redirect("UserList.ashx");
}
else
{
context.Response.Redirect("error.html");
}
}

}
}
else
{
context.Response.Write("参数错误");
}
}
/// <summary>
/// 返回数据库的连接字符串
/// </summary>
/// <returns></returns>
public string GetConnStr()
{
string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
return ConnStr;
}
public bool IsReusable {
get {
return false;
}
}

}

 

error.htm脚本页面错误跳转

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>

</head>
<body>
程序错误:<span id="time">5</span>秒钟跳到<a href="http://www.mamicode.com/UserList.ashx" id="url">跳转到首页</a>
<script type="text/javascript">
setTimeout(changCount, 1000);

function changCount() {
var time = document.getElementById("time").innerHTML;
time = parseInt(time);
time--;
if (time < 1) {
var url = document.getElementById("url").href;
window.location.href = http://www.mamicode.com/url;
} else {
document.getElementById("time").innerHTML = time;
setTimeout(changCount, 1000);
}
}
</script>
</body>
</html>

ASP.NET 一般处理程序删除数据