首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何生成所需的SQL语句以更新、插入、删除GridView中的数据?

问如何生成所需的SQL语句以更新、插入、删除GridView中的数据?
EN

Stack Overflow用户
提问于 2015-12-30 12:15:47
回答 1查看 1.5K关注 0票数 1

我使用GridView和SqlDataSource,用于选择、更新和删除,以及针对插入E 210的DetailsView,问题是,当我单击这些按钮之一时,就会得到以下错误:Delete顺序(以及与Insert和Update相同的错误):

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

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

源代码:

代码语言:javascript
复制
<%@ 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>
EN

回答 1

Stack Overflow用户

发布于 2015-12-30 12:19:16

将DeleteCommand添加到SqlDataSource中

代码语言:javascript
复制
DeleteCommand="Delete from yourTable WHERE (ID = @ID)"

如下所示:

代码语言:javascript
复制
<asp:SqlDataSource runat="server" DeleteCommand="Delete from yourTable WHERE (ID = @ID)">
    <DeleteParameters>
        <asp:Parameter Name="ID"></asp:Parameter>
    </DeleteParameters>
</asp:SqlDataSource>

编辑:使ASP.NET无需任何代码就自动生成DELETE命令,执行以下步骤:

  1. 在SqlDataSource的“任务”窗口中,单击“配置数据源”。
  2. 选择您的数据连接和Next。
  3. 确保指定选中的自定义SQL语句或存储过程,然后单击Next。
  4. 选择删除选项卡,然后单击查询生成器,选择表,然后通过帮助查询生成器窗口构建查询。最后,您的查询应该如下所示: 从yourTable中删除(ID = @ID)
  5. 最后单击Next和Finish。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34529385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档