当创建Delete触发器时,删除前的数据会自动导入Deleted临时表中,可能通过查询Deleted临时表获取删除前的数据
SQL
View Code
/*打开数据库*/ USE db_Tome2 /*判断表中是否有名为employee_delete的触发器*/ IF EXISTS (SELECT name FROM sysobjects WHERE name = 'song_delete' AND type = 'TR') /*如果已经存在则删除*/ DROP TRIGGER song_delete GO /*创建新的删除触发器*/ CREATE TRIGGER song_delete ON tb_DeleteTR FOR DELETE AS BEGIN IF @@rowcount > 1 BEGIN ROLLBACK TRANSACTION RAISERROR ('你每次只能删除一条记录',16, 1) END END GO
Default.aspx
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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 id="Head1" runat="server"> <title>无标题页</title> <link href="StyleSheet.css" rel="stylesheet" type="text/css" /> </head> <body style="text-align: center"> <form id="form1" runat="server"> <div> <table cellpadding="0" cellspacing="0" style="width: 521px; background-image: url(Images/图片2 拷贝.jpg); border-left-color: #ffcc66; border-bottom-color: #ffcc66; border-top-style: ridge; border-top-color: #ffcc66; border-right-style: ridge; border-left-style: ridge; border-right-color: #ffcc66; border-bottom-style: ridge;"> <tr> <td style="height: 83px"> </td> <td style="width: 60378px; height: 83px"> </td> <td style="width: 905px; height: 83px"> </td> <td style="width: 92px; height: 83px"> </td> </tr> <tr> <td style="height: 13px"> </td> <td style="background-image: url(Images/背景图10.jpg); width: 60378px; height: 13px"> <asp:Label ID="Label1" runat="server" BackColor="#FFE0C0" Text="明日科技"></asp:Label></td> <td rowspan="3" style="width: 905px"> <table cellpadding="0" cellspacing="0" style="width: 332px; background-image: url(Images/1294850_472733.gif);"> <tr> <td colspan="2" style="height: 141px"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellSpacing="2" Font-Size="9pt" Width="377px" AllowPaging="True" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="7"> <Columns> <asp:BoundField DataField="id" HeaderText="编号" /> <asp:BoundField DataField="name" HeaderText="名星人物" /> <asp:BoundField DataField="age" HeaderText="打榜次数" /> <asp:BoundField DataField="sex" HeaderText="歌曲名称" /> <asp:BoundField DataField="zhishu" HeaderText="人气指数" /> <asp:BoundField DataField="datetime" HeaderText="上榜时间" /> </Columns> <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /><FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> </asp:GridView> </td> </tr> <tr> <td colspan="2" style="height: 42px"> <table bgcolor="#ccffff" cellpadding="0" cellspacing="0" style="width: 355px; height: 40px; background-color: #ffcc99;"> <tr> <td style="width: 57px"> <asp:Label ID="Label2" runat="server" Font-Size="9pt" Text="删除" Width="42px"></asp:Label></td> <td style="width: 69px"> <asp:DropDownList ID="DropDownList1" runat="server" CssClass="ddl" Height="28px" ><asp:ListItem>打榜次数</asp:ListItem> <asp:ListItem>人气指数</asp:ListItem> </asp:DropDownList></td> <td style="width: 38px"> <asp:Label ID="Label3" runat="server" Font-Size="9pt" Text="为"></asp:Label></td> <td style="width: 75px"> <asp:TextBox ID="TextBox1" runat="server" CssClass="txt" BackColor="#E0E0E0" Height="22px"></asp:TextBox></td> <td align="center" style="width: 80px"> <asp:Label ID="Label4" runat="server" Font-Size="9pt" Text="的记录"></asp:Label></td> <td style="width: 48px"> <asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="确定" Width="48px" /></td> </tr> </table> </td> </tr> <tr> <td colspan="2"> <span style="font-size: 11pt">吉林省明日科技有限公司</span></td> </tr> </table> </td> <td style="width: 92px; height: 13px"> </td> </tr> <tr> <td style="height: 2px"> </td> <td style="background-image: url(Images/背景图10.jpg); width: 60378px; height: 2px"> <asp:Label ID="Label5" runat="server" BackColor="#FFE0C0" Text="成就未来明星"></asp:Label></td> <td style="width: 92px; height: 2px"> </td> </tr> <tr> <td style="height: 178px"> </td> <td style="background-image: url(Images/背景图10.jpg); width: 60378px; height: 178px"> <span style="color: #990033"> <br /> <br /> <br /> <br /> </span> </td> <td style="width: 92px; height: 178px"> </td> </tr> <tr> <td> </td> <td rowspan="3" style="width: 60378px"> </td> <td rowspan="3" style="width: 905px"> </td> <td style="width: 92px"> </td> </tr> <tr> <td> </td> <td style="width: 92px"> </td> </tr> <tr> <td> </td> <td style="width: 92px;"> </td> </tr> </table> </div> </form> </body> </html>
Default.asp.cs
View Code
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]); con.Open(); SqlDataAdapter ada = new SqlDataAdapter("select * from tb_DeleteTR", con); DataSet ds = new DataSet(); ada.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); } //自定义一个GetDataSet方法,返回DataSet数据集 public DataSet GetDataSet(string sqlstr) { SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]); con.Open(); SqlCommand com = new SqlCommand(sqlstr, con); com.ExecuteNonQuery(); SqlDataAdapter ada = new SqlDataAdapter("select * from tb_DeleteTR", con); DataSet ds = new DataSet(); ada.Fill(ds); con.Close(); return ds; } protected void Button1_Click(object sender, EventArgs e) { if (DropDownList1.Text == "打榜次数") { try { string sqlstr1 = "delete from tb_DeleteTR where age='" + TextBox1.Text + "'"; GridView1.DataSource = GetDataSet(sqlstr1).Tables[0].DefaultView; GridView1.DataBind(); Response.Write("<script language=javascript>alert('删除成功!')</script>"); } catch { Response.Write("<script language=javascript>alert('不可批量删除,你每次只能删除一条记录!')</script>"); } } if (DropDownList1.Text == "人气指数") { try { string sqlstr2 = "delete from tb_DeleteTR where zhishu='" + TextBox1.Text + "'"; GetDataSet(sqlstr2); GridView1.DataSource = GetDataSet(sqlstr2).Tables[0].DefaultView; GridView1.DataBind(); Response.Write("<script language=javascript>alert('删除成功!')</script>"); } catch { Response.Write("<script language=javascript>alert('不可批量删除,你每次只能删除一条记录!')</script>"); } } } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridView1.DataBind(); } }