基于SqlDataSource的DataView中的删除操作
1 创建支持删除的SqlDataSource
在配置SqlDataSource的Sql语句后,左下角的高级
点击高级选项后出现如下画面
完成后会自动生成基于主键的InsertCommand ,DeleteCommand,UpdateCommand的语句,如下
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WHGDTempConnectionString2 %>"
DeleteCommand="DELETE FROM [AddrInfo] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [AddrInfo] ([TVNum], [HostName], [TerminalNum], [Addr]) VALUES (@TVNum, @HostName, @TerminalNum, @Addr)"
SelectCommand="SELECT [ID], [TVNum], [HostName], [TerminalNum], [Addr] FROM [AddrInfo]"
UpdateCommand="UPDATE [AddrInfo] SET [TVNum] = @TVNum, [HostName] = @HostName, [TerminalNum] = @TerminalNum, [Addr] = @Addr WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Decimal" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="TVNum" Type="Decimal" />
<asp:Parameter Name="HostName" Type="String" />
<asp:Parameter Name="TerminalNum" Type="String" />
<asp:Parameter Name="Addr" Type="String" />
<asp:Parameter Name="ID" Type="Decimal" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="TVNum" Type="Decimal" />
<asp:Parameter Name="HostName" Type="String" />
<asp:Parameter Name="TerminalNum" Type="String" />
<asp:Parameter Name="Addr" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
DeleteCommand="DELETE FROM [AddrInfo] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [AddrInfo] ([TVNum], [HostName], [TerminalNum], [Addr]) VALUES (@TVNum, @HostName, @TerminalNum, @Addr)"
SelectCommand="SELECT [ID], [TVNum], [HostName], [TerminalNum], [Addr] FROM [AddrInfo]"
UpdateCommand="UPDATE [AddrInfo] SET [TVNum] = @TVNum, [HostName] = @HostName, [TerminalNum] = @TerminalNum, [Addr] = @Addr WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Decimal" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="TVNum" Type="Decimal" />
<asp:Parameter Name="HostName" Type="String" />
<asp:Parameter Name="TerminalNum" Type="String" />
<asp:Parameter Name="Addr" Type="String" />
<asp:Parameter Name="ID" Type="Decimal" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="TVNum" Type="Decimal" />
<asp:Parameter Name="HostName" Type="String" />
<asp:Parameter Name="TerminalNum" Type="String" />
<asp:Parameter Name="Addr" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
上面的语句中,ID为主键,至此SqlDataSource
创建完成
2 生成DataView
编辑列时,添加一个绑定字段和一个删除字段
添加列完成后,为DataView的DataKeyNames属性添加值ID,这个是保证删除操作能够完成的关键
也就是和SqlDataSource中的字段相对应