0


我翻遍了,研究了书籍,教程视频和大量文章,但我无法解决这个问题。在 Visual Studio 2010 中插入一个网格视图,我将数据源指定为访问数据库。在规范中,我包括插入、更新和删除记录的选项。网格视图出现在屏幕上,当我测试网页时,它会加载更新和插入等相关按钮。测试页面时,我单击更新按钮,更改一行,然后单击更新。页面恢复到以前的状态,记录保持不变。我刷新页面以确保它没有更新,它肯定没有。我完全按照教程进行操作,使用此方法时无法更新或删除字段。我使用的数据库是一个访问数据库,一个 mdb 文件。

我确定我在这里遗漏了一些非常简单的东西,但我不确定是什么。如果有人可以提供一些帮助,我将不胜感激!谢谢

这是我的表单代码。

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="Book Id" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                ShowSelectButton="True" />
            <asp:BoundField DataField="Book Id" HeaderText="Book Id" InsertVisible="False" 
                ReadOnly="True" SortExpression="Book Id" />
            <asp:BoundField DataField="ISBN Number" HeaderText="ISBN Number" 
                SortExpression="ISBN Number" />
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            <asp:BoundField DataField="Author" HeaderText="Author" 
                SortExpression="Author" />
            <asp:BoundField DataField="Publisher" HeaderText="Publisher" 
                SortExpression="Publisher" />
            <asp:BoundField DataField="Date Published" HeaderText="Date Published" 
                SortExpression="Date Published" />
            <asp:CheckBoxField DataField="Availability" HeaderText="Availability" 
                SortExpression="Availability" />
            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:booksellerConnectionString %>" 
        DeleteCommand="DELETE FROM [tblBooks] WHERE [Book Id] = ? AND (([ISBN Number] = ?) OR ([ISBN Number] IS NULL AND ? IS NULL)) AND (([Title] = ?) OR ([Title] IS NULL AND ? IS NULL)) AND (([Author] = ?) OR ([Author] IS NULL AND ? IS NULL)) AND (([Publisher] = ?) OR ([Publisher] IS NULL AND ? IS NULL)) AND (([Date Published] = ?) OR ([Date Published] IS NULL AND ? IS NULL)) AND [Availability] = ? AND (([Price] = ?) OR ([Price] IS NULL AND ? IS NULL))" 
        InsertCommand="INSERT INTO [tblBooks] ([Book Id], [ISBN Number], [Title], [Author], [Publisher], [Date Published], [Availability], [Price]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)" 
        OldValuesParameterFormatString="original_{0}" 
        ProviderName="<%$ ConnectionStrings:booksellerConnectionString.ProviderName %>" 
        SelectCommand="SELECT * FROM [tblBooks]" 
        UpdateCommand="UPDATE [tblBooks] SET [ISBN Number] = ?, [Title] = ?, [Author] = ?, [Publisher] = ?, [Date Published] = ?, [Availability] = ?, [Price] = ? WHERE [Book Id] = ? AND (([ISBN Number] = ?) OR ([ISBN Number] IS NULL AND ? IS NULL)) AND (([Title] = ?) OR ([Title] IS NULL AND ? IS NULL)) AND (([Author] = ?) OR ([Author] IS NULL AND ? IS NULL)) AND (([Publisher] = ?) OR ([Publisher] IS NULL AND ? IS NULL)) AND (([Date Published] = ?) OR ([Date Published] IS NULL AND ? IS NULL)) AND [Availability] = ? AND (([Price] = ?) OR ([Price] IS NULL AND ? IS NULL))">
        <DeleteParameters>
            <asp:Parameter Name="original_Book_Id" Type="Int32" />
            <asp:Parameter Name="original_ISBN_Number" Type="String" />
            <asp:Parameter Name="original_ISBN_Number" Type="String" />
            <asp:Parameter Name="original_Title" Type="String" />
            <asp:Parameter Name="original_Title" Type="String" />
            <asp:Parameter Name="original_Author" Type="String" />
            <asp:Parameter Name="original_Author" Type="String" />
            <asp:Parameter Name="original_Publisher" Type="String" />
            <asp:Parameter Name="original_Publisher" Type="String" />
            <asp:Parameter Name="original_Date_Published" Type="String" />
            <asp:Parameter Name="original_Date_Published" Type="String" />
            <asp:Parameter Name="original_Availability" Type="Boolean" />
            <asp:Parameter Name="original_Price" Type="Decimal" />
            <asp:Parameter Name="original_Price" Type="Decimal" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Book_Id" Type="Int32" />
            <asp:Parameter Name="ISBN_Number" Type="String" />
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Author" Type="String" />
            <asp:Parameter Name="Publisher" Type="String" />
            <asp:Parameter Name="Date_Published" Type="String" />
            <asp:Parameter Name="Availability" Type="Boolean" />
            <asp:Parameter Name="Price" Type="Decimal" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="ISBN_Number" Type="String" />
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Author" Type="String" />
            <asp:Parameter Name="Publisher" Type="String" />
            <asp:Parameter Name="Date_Published" Type="String" />
            <asp:Parameter Name="Availability" Type="Boolean" />
            <asp:Parameter Name="Price" Type="Decimal" />
            <asp:Parameter Name="original_Book_Id" Type="Int32" />
            <asp:Parameter Name="original_ISBN_Number" Type="String" />
            <asp:Parameter Name="original_ISBN_Number" Type="String" />
            <asp:Parameter Name="original_Title" Type="String" />
            <asp:Parameter Name="original_Title" Type="String" />
            <asp:Parameter Name="original_Author" Type="String" />
            <asp:Parameter Name="original_Author" Type="String" />
            <asp:Parameter Name="original_Publisher" Type="String" />
            <asp:Parameter Name="original_Publisher" Type="String" />
            <asp:Parameter Name="original_Date_Published" Type="String" />
            <asp:Parameter Name="original_Date_Published" Type="String" />
            <asp:Parameter Name="original_Availability" Type="Boolean" />
            <asp:Parameter Name="original_Price" Type="Decimal" />
            <asp:Parameter Name="original_Price" Type="Decimal" />
        </UpdateParameters>
    </asp:SqlDataSource>

下面的 asp.cs 文件;

namespace Second
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
4

2 回答 2

0

我不一定有一个直接的答案,但有一些建议:

这可能是因为您的复杂性UPDATE

UPDATE [tblBooks] SET [ISBN Number] = ?, [Title] = ?, [Author] = ?, [Publisher] = ?, [Date Published] = ?, [Availability] = ?, [Price] = ? WHERE [Book Id] = ? AND (([ISBN Number] = ?) OR ([ISBN Number] IS NULL AND ? IS NULL)) AND (([Title] = ?) OR ([Title] IS NULL AND ? IS NULL)) AND (([Author] = ?) OR ([Author] IS NULL AND ? IS NULL)) AND (([Publisher] = ?) OR ([Publisher] IS NULL AND ? IS NULL)) AND (([Date Published] = ?) OR ([Date Published] IS NULL AND ? IS NULL)) AND [Availability] = ? AND (([Price] = ?) OR ([Price] IS NULL AND ? IS NULL))

因为你有一个相当沉重的WHERE条款。如果您只是将其缩减为:

UPDATE [tblBooks] SET [ISBN Number] = ?, [Title] = ?, [Author] = ?, [Publisher] = ?, [Date Published] = ?, [Availability] = ?, [Price] = ? WHERE [Book Id] = ?

并相应地削减你的<UpdateParameters>,这有什么影响吗?

另外(对我来说已经有一段时间了)我注意到您的参数名称(例如Book_Id)与数据字段绑定(例如)不完全匹配,Book Id因此可能会导致问题。我相信他们需要匹配(或者他们这样做是好的)以实现绑定目的,但在这种情况下他们不需要。

可能值得修改您的数据库,以便列名类似于 [Book_Id]、[ISBN_Number] 等...我认为数据库列中很少有空格(根据我的经验当然很少见...但我是没那么有经验!)

于 2013-03-27T21:55:36.367 回答
0

只是为了踢,尝试删除线 ConflictDetection="CompareAllValues"。现在会更新吗?

于 2013-03-28T05:35:17.367 回答