【问题标题】:How to generate the needed SQL statements to Update, Insert, Delete data in GridView?如何在 GridView 中生成更新、插入、删除数据所需的 SQL 语句?
【发布时间】:2016-04-04 10:04:31
【问题描述】:

我将GridViewSqlDataSource 一起用于选择更新删除, 和DetailsView 用于插入,问题是,当我单击其中一个按钮时,Delete 订单出现此错误(InsertUpdate 出现相同错误。 .):

数据源“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>

【问题讨论】:

  • 你能告诉我们你所做的代码吗?
  • 参见 SQLdatasource1 仅包含 Selectcommand,您必须包含所有其他命令(插入、更新、删除),如 @user2946329 答案。
  • 这不能通过为 SqlDataSouce 启用插入、删除和更新来自动完成吗?
  • @MohammedElshawaf...您也可以通过单击SqlDataSource 中的配置数据源来执行此操作。
  • @MohammedElshawaf...正如我所说:要让 ASP.NET 无需任何代码自动为您生成 DELETE 命令,您应该使用配置数据源。我用完整的演练更新了我的答案。

标签: c# asp.net sql-server gridview sqldatasource


【解决方案1】:

DeleteCommand 添加到您的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>

编辑:要让 ASP.NET 自动为您生成 DELETE 命令,无需任何代码,请执行以下步骤:

  1. SqlDataSource 的任务窗口中,点击配置数据源
  2. 选择您的数据连接并下一步
  3. 确保选中指定自定义 SQL 语句或存储过程,然后单击下一步
  4. 选择 DELETE 选项卡,然后单击 Query Builder,选择您的表,然后通过 Query Builder 窗口构建您的查询。你的查询最终应该是这样的:

    Delete from yourTable WHERE (ID = @ID)
    
  5. 最后点击NextFinish

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多