1

我有一个显示表中的记录集的gridview - Master2。我想使用 gridview 从这个表中删除记录。在删除发生之前,我想将记录插入到历史记录表中,其中包含执行删除操作的人的日期时间戳和用户 ID。这是可能的还是我应该简单地用一系列下拉框和一个按钮来构建字段?

4

3 回答 3

1

使用FOR DELETE触发器:

CREATE TRIGGER TriggerName    
ON TableName  
FOR DELETE    
AS     
BEGIN    
    INSERT INTO HistoryTable(Col1, Col2, Col3)  
    SELECT Col1, GETDATE(), Col3  
    FROM TableName  
    WHERE DeletedRecordID IN (SELECT DeletedRecordID FROM TableName)     
END  
于 2013-03-07T15:18:35.050 回答
0

使用触发器。

更安全,这意味着每次您从该历史表中删除一条记录(无论机制如何)时,它仍然会触发。

它也更容易编码,因为您不必在 .net 代码中放入任何内容来处理它。

最后,由于涉及零网络流量,因此速度更快。

于 2013-03-07T14:34:36.047 回答
0

I recomend using a flag for deletion, and not actually delete the record... to the gridview, you add an extra checkpoint that when selected it will tr​​igger...like this

<asp:TemplateField HeaderText="Delete" SortExpression="ToBeDeleted">
                            <EditItemTemplate>
                                <asp:TextBox ID="txtToBeDeleted" runat="server" Text='<%# Bind("ToBeDeleted") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <%--<asp:Label ID="lblToBeDeleted" Width="20px" runat="server" Text='<%# Bind("ToBeDeleted") %>'></asp:Label>--%>
                                <asp:CheckBox ID="chkToBeDeleted" Width="20px" runat="server" Checked='<%# Bind("ToBeDeleted") %>' OnCheckedChanged="chkToBeDeleted_CheckedChanged" AutoPostBack="true"/>
                            </ItemTemplate>
                            <ItemStyle CssClass="overflowClass" HorizontalAlign="Center" Wrap="false" />
                        </asp:TemplateField>

然后在你处理触发器的代码中:

protected void chkToBeDeleted_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            GridViewRow gvr = (GridViewRow)cb.NamingContainer;
            int id = (int)gvWines.DataKeys[gvr.RowIndex].Value;

            ListOfWine record = (from l in db.ListOfWines
                         where l.Id == id
                         select l).FirstOrDefault();

            //here you add the record to another table and then continue flagging the record for deletion

         record.ToBeDeleted = cb.Checked;
db.SaveChanges();
            gvWines.DataBind();
        }
于 2013-03-07T14:38:13.740 回答