IDENTITY_INSERT设置为OFF

时间:2017-04-15 01:59:36

标签: c# sql-server

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CssClass="table table-striped"  DataKeyNames="productsID" DataSourceID="productsObj">
    <Columns>
            <asp:BoundField DataField="productsID" HeaderText="productsID" ReadOnly="True" SortExpression="productsID" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
            <asp:BoundField DataField="image" HeaderText="image" SortExpression="image" />
    </Columns>
</asp:GridView>
<asp:ObjectDataSource ID="productsObj" runat="server" 
     DeleteMethod="Delete" InsertMethod="Insert" 
     OldValuesParameterFormatString="original_{0}" 
     SelectMethod="GetData" 
     TypeName="productsTableAdapters.productsTableAdapter" 
     UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_productsID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="productsID" Type="Int32" />
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="price" Type="Decimal" />
            <asp:Parameter Name="image" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="price" Type="Decimal" />
            <asp:Parameter Name="image" Type="String" />
            <asp:Parameter Name="Original_productsID" Type="Int32" />
        </UpdateParameters>
</asp:ObjectDataSource>

<div class="col-lg-10 col-lg-offset-1 col-md-12">
    <div class="col-lg-6">
        <asp:Label ID="Label1" CssClass="h1" runat="server" Text="Add Albums"></asp:Label>
        <asp:DetailsView ID="DetailsView1" runat="server" CssClass="table table-striped" AutoGenerateRows="False" DataKeyNames="productsID" DataSourceID="productsObj" DefaultMode="Insert">
            <Fields>
                <asp:BoundField DataField="productsID" HeaderText="productsID" ReadOnly="True" SortExpression="productsID" InsertVisible="False" />
                <asp:BoundField DataField="name" HeaderText="name" ControlStyle-CssClass="input-sm form-control" SortExpression="name" />
                <asp:BoundField DataField="price" HeaderText="price" ControlStyle-CssClass=" input-sm form-control" SortExpression="price" />
                <asp:BoundField DataField="image" HeaderText="image" ControlStyle-CssClass=" input-sm form-control" SortExpression="image" />
                <asp:CommandField ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
    </div>

SQL Server表结构:

CREATE TABLE [dbo].[products] 
(
    [productsID] INT IDENTITY (1, 1) NOT NULL,
    [name] VARCHAR(50) NOT NULL,
    [price] DECIMAL(18, 2) NOT NULL,
    [image] VARCHAR(255) NOT NULL,

    PRIMARY KEY CLUSTERED ([productsID] ASC)
);

我有一个显示一些数据的gridview,我知道希望通过详细信息视图输入一些新行。由于一些奇怪的原因,我一直收到这个错误:

  

当IDENTITY_INSERT设置为OFF时,无法在表'products'中为identity列插入显式值。

我真的不知道是什么导致它这样做,所以我坚持寻求解决方案。任何帮助或建议都会很棒!

1 个答案:

答案 0 :(得分:1)

错误的原因是,您尝试将值插入带有标识列的表并且SQL建议,如果您想这样做,则必须启用

SET IDENTITY_INSERT ON;

如果您不希望将插入值插入标识列,则可以执行以下操作

insert into table
(all columns except identity)
values
(col values for all except identity)