从DataGridView的SQL删除没有发生
本文关键字:删除 DataGridView SQL | 更新日期: 2023-09-27 18:18:05
我有一个DataGridView已删除启用,但它从未发生。整个网格消失(多行),但是我所单击的行永远不会被删除。
我可以在sql管理工作室手动运行sql语句,它工作得很好,但似乎从来没有从网页运行。
我不知道从这里去哪里,我没有这样的问题从GridView之前。
SqlDataSource:
<asp:SqlDataSource ID="SqlDataSourceVisitors" runat="server" ConnectionString="constring" DeleteCommand="DELETE FROM [RegisteredVisitors] WHERE [PK] = @PK UPDATE [dbo].[Users] SET [NumberOfVisitors] = (SELECT [NumberOfVisitors] FROM [dbo].[NumberOfVisitors] WHERE [ResidentName] = @ResidentName) -1 WHERE [ResidentName] = @ResidentName" InsertCommand="INSERT INTO [RegisteredVisitors] ([ResidentName], [ResidentAddress], [VisitorCode], [VisitorName]) VALUES (@ResidentName, @ResidentAddress, @VisitorCode, @VisitorName)" SelectCommand="SELECT * FROM [RegisteredVisitors] WHERE ([ResidentName] = @ResidentName)" UpdateCommand="UPDATE [RegisteredVisitors] SET [ResidentName] = @ResidentName, [ResidentAddress] = @ResidentAddress, [VisitorCode] = @VisitorCode, [VisitorName] = @VisitorName WHERE [PK] = @PK" ProviderName="System.Data.SqlClient">
<DeleteParameters>
<asp:Parameter Name="PK" Type="Int32" />
<asp:Parameter Name="ResidentName" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="ResidentName" Type="String" />
<asp:Parameter Name="ResidentAddress" Type="String" />
<asp:Parameter Name="VisitorCode" Type="Int32" />
<asp:Parameter Name="VisitorName" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="txtboxSelectedResident" Name="ResidentName" PropertyName="Text" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ResidentName" Type="String" />
<asp:Parameter Name="ResidentAddress" Type="String" />
<asp:Parameter Name="VisitorCode" Type="Int32" />
<asp:Parameter Name="VisitorName" Type="String" />
<asp:Parameter Name="PK" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
GridView: <asp:GridView ID="DataGridViewVisitors" runat="server" CellPadding="4" DataSourceID="SqlDataSourceVisitors" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
如果我是正确的,您只是在DeleteCommand中错过了;。所有写在command里面的东西都会一次执行。因此,要将删除和更新查询分开,您需要;
DeleteCommand="DELETE FROM [RegisteredVisitors] WHERE [PK] = @PK; UPDATE [dbo].[Users] SET [NumberOfVisitors] = (SELECT [NumberOfVisitors] FROM [dbo].[NumberOfVisitors] WHERE [ResidentName] = @ResidentName) -1 WHERE [ResidentName] = @ResidentName"