删除数据库中的一条数据

 1 <%@ WebHandler Language="C#" Class="List" %>
 2 
 3 using System;
 4 using System.Web;
 5 
 6 
 7 using System.Data;
 8 using System.Data.SqlClient;
 9 using System.Text;
10 using System.IO;
11 
12 public class List : IHttpHandler {
13     
14     public void ProcessRequest (HttpContext context) {
15         //context.Response.ContentType = "text/plain";
16         //context.Response.Write("Hello World");
17         DataTable dt = LoadData();
18         StringBuilder sb = new StringBuilder("<table style = \"border:1px\">");
19         foreach(DataRow dr in dt.Rows)
20         {
21             sb.Append("<tr>");
22             sb.Append("<td>"+ dr[0]+"</td>");
23             sb.Append("<td>"+ dr[1]+"</td>");
24             sb.Append("<td>"+ dr[2]+"</td>");
25             sb.Append("<td><a href='del.ashx?id="+dr[0].ToString()+"'>删除</a></td>");
26             sb.Append("</tr>");
27         }
28         sb.Append("</table>");
29         string strPath = context.Server.MapPath("./HtmlModel.htm");
30         string strHtml = File.ReadAllText(strPath);
31         strHtml= strHtml.Replace("@title","欢迎查看学生列表页");

32         strHtml= strHtml.Replace("@body", sb.ToString());
33         context.Response.Write(strHtml);
34     }
35 
36     private DataTable LoadData()
37     {
38         DataTable dt = null;
39         try
40         {
41             SqlConnection conn = new SqlConnection("Data Source=PC--20130405SCI\\YAOSIR;Initial Catalog=Clients;User ID= sa;Password=123456");
42 
43             conn.Open();
44             SqlCommand cmd = new SqlCommand();
45            
46                 cmd.CommandText = "Select * from OrderClient";
47 
48                 cmd.Connection = conn;
49                 SqlDataReader dr = cmd.ExecuteReader();
50                     //SqlDataAdapter da = new SqlDataAdapter(selectStr,conn);
51                 dt = new DataTable();
52                 dt.Load(dr);
53 
54             
55             //string sqlStr = "Select * from OrderClient";
56             //SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn);
57             //dt = new DataTable();
58             //da.Fill(dt);
59         }
60         catch (Exception e )
61         {
62             
63             throw new Exception("执行出现异常...");
64         }
65         
66         return dt;
67     }
68  
69     public bool IsReusable {
70         get {
71             return false;
72         }
73     }
74 
75 }

点击“删除”跳转到del.ashx页面

 1 <%@ WebHandler Language="C#" Class="del" %>
 2 
 3 using System;
 4 using System.Web;
 5 using System.Data;
 6 using System.Data.SqlClient;
 7 
 8 public class del : IHttpHandler {
 9     
10     public void ProcessRequest (HttpContext context) {
11         //context.Response.ContentType = "text/plain";
12         //context.Response.Write("Hello World");
13         string sid = context.Request.QueryString["id"];
14         if (sid != null)
15         {
16             int ID = -1;
17             if (int.TryParse(sid, out ID))
18             {
19                 if (DoDel(ID))
20                 {
21                     context.Response.Write("删除成功!");
22                 }
23                 else
24                 {
25                     context.Response.Write("删除失败!");
26                 }
27             }
28             else
29             {
30                 context.Response.Write("主键转换失败....");
31             }
32         }
33         else
34         {
35             context.Response.Write("未获取主键值!"); 
36         }
37     }
38     private bool DoDel(int id)
39     {
40         string connStr = "Data Source=PC--20130405SCI\\YAOSIR;Initial Catalog=Clients;User ID= sa;Password=123456";
41         SqlConnection conn = new SqlConnection(connStr);
42         conn.Open();
43         string sqlStr = "delete from OrderClient where ClientID = @sid";
44         SqlCommand cmd = new SqlCommand(sqlStr, conn);
45         SqlParameter par = new SqlParameter("@sid",id);
46         cmd.Parameters.Add(par);
47         int result = cmd.ExecuteNonQuery();
48         
49         return result>0; 
50     }
51  
52     public bool IsReusable {
53         get {
54             return false;
55         }
56     }
57 
58 }

 

转载于:https://www.cnblogs.com/yaoxc/archive/2013/05/26/3100535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值