C# 如何生成所需的SQL语句来更新、插入和删除GridView中的数据?

C# 如何生成所需的SQL语句来更新、插入和删除GridView中的数据?,c#,asp.net,sql-server,gridview,sqldatasource,C#,Asp.net,Sql Server,Gridview,Sqldatasource,我正在使用GridView和SqlDataSource,用于选择,更新和删除, 和DetailsView对于插入,问题是,当我单击其中一个按钮时,我得到了删除订单的此错误(与插入和更新的错误相同): 数据源“SqlDataSource1”不支持删除,除非 已指定DeleteCommand 我自己添加这些语句非常累人,在某些情况下,我需要定义存储过程。我这样问是因为我读了一本教程(但不幸的是,我不记得在哪里),Visual Studio会自动生成所有这些教程 源代码: <%@ Page Ti

我正在使用
GridView
SqlDataSource
,用于选择更新删除, 和
DetailsView
对于插入,问题是,当我单击其中一个按钮时,我得到了
删除
订单的此错误(与
插入
更新
的错误相同):

数据源“SqlDataSource1”不支持删除,除非 已指定DeleteCommand

我自己添加这些语句非常累人,在某些情况下,我需要定义存储过程。我这样问是因为我读了一本教程(但不幸的是,我不记得在哪里),Visual Studio会自动生成所有这些教程

源代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Donor.aspx.cs" Inherits="Donor" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:GridView ID="GridView1" runat="server"  
         AutoGenerateColumns="False" CellPadding="4" 
         DataKeyNames="Id" DataSourceID="SqlDataSource1" 
         EmptyDataText="There are no data records to display." 
         ForeColor="#333333" GridLines="None" AllowSorting="True">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ItemStyle-Width="120px"/>
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"  ItemStyle-Width="150px"/>
            <asp:BoundField DataField="BloodGroup" HeaderText="BloodGroup" SortExpression="BloodGroup"  ItemStyle-Width="120px"/>
            <asp:BoundField DataField="Disease" HeaderText="Disease" SortExpression="Disease"  ItemStyle-Width="120px"/>
        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
         ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
         SelectCommand="SELECT [Name], [BloodGroup], [Disease], [Id] FROM [Patient]">
    </asp:SqlDataSource>
    <asp:DetailsView ID="DetailsView1" runat="server" CellPadding="4" 
         DataSourceID="SqlDataSource2" ForeColor="#333333" 
         GridLines="None" Height="50px" Width="125px" 
         DefaultMode="Insert">
        <AlternatingRowStyle BackColor="White" />
        <CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />
        <FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
        <Fields>
            <asp:CommandField ShowInsertButton="True" />
        </Fields>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Name], [BloodGroup], [Disease] FROM [Patient]" InsertCommand="INSERT INTO [Patient] ([Name],[BloodGroup],[Disease]) VALUES (@Name, @BloodGroup, @Disease)">
        <InsertParameters>
            <asp:Parameter Name="Name" />
            <asp:Parameter Name="BloodGroup" />
            <asp:Parameter Name="Disease" />
        </InsertParameters>
    </asp:SqlDataSource>
</asp:Content>

delete命令添加到
SqlDataSource

DeleteCommand="Delete from yourTable WHERE (ID = @ID)"
像这样:

<asp:SqlDataSource runat="server" DeleteCommand="Delete from yourTable WHERE (ID = @ID)">
    <DeleteParameters>
        <asp:Parameter Name="ID"></asp:Parameter>
    </DeleteParameters>
</asp:SqlDataSource>
  • 最后单击下一步和完成


  • 你能告诉我们你做了什么吗?请看在SQLdatasource1中只包含Selectcommand,你必须包含所有其他命令(插入、更新、删除),就像@user2946329 Answer一样。这不能通过启用插入自动完成吗,删除并更新SqlDataSouce?@MohammedElshawaf…您也可以通过单击
    SqlDataSource
    @MohammedElshawaf中的配置数据源来完成此操作…正如我所说:要使ASP.NET自动为您生成删除命令,而无需任何代码,您应该使用配置数据源。我用完整的演练更新了我的答案。
    Delete from yourTable WHERE (ID = @ID)