简体   繁体   English

ASP.NET SQL Server插入错误

[英]ASP.NET SQL Server insert error

So I've these two statements: 因此,我有以下两个陈述:

         string insertUserData = "INSERT INTO W711_User_Data(Network_ID, F_Name, M_Name, L_NAME, Badge, Telephone, Org_Code, Org_Name, Req_Head_Network_ID)Values(@networkID1, @firstName1, @middleName1, @lastName1, @badgeNumber1, @telephone1, @orgCode1, @orgName1, @myUserName1)";


        string insertReservationData = "INSERT INTO W711_Reservation_Data(ID, Network_ID, EventTitle, StartDate, EndDate, Justification) Values(null, @networkID2, @eventTitle1, @startDate1, @endDate1, @justification1)"; 

The network id in second string is foreign key relation with network id in first table. 第二个字符串中的网络ID是与第一个表中的网络ID的外键关系。

The problem is: When I run the application in VS2010 , it gives the error: Can't insert explicit value for identity column in table 'W711_Reservation_Data' when IDENTITY_INSERT is set to OFF . 问题是:当我在VS2010运行该application时,出现错误: IDENTITY_INSERT设置为OFF时,无法在表'W711_Reservation_Data'为Identity column插入显式值。

Then I read to do this somewhere: 然后我读到要在某处这样做:

SET IDENTITY_INSERT W711_Reservation_Data ON

But it also fails and gives the same error again! 但是它也会失败并再次给出相同的错误! Please help Thanks ps sql server 请帮忙谢谢ps sql server

Why are you trying to insert null as the value for the ID column anyway? 无论如何,为什么要插入null作为ID列的值? Assuming that this is the IDENTITY column that is the source of the complaint then it seems more likely you need to just leave it out of the column list and don't pass any explicit value in. ie 假设这是投诉的来源是IDENTITY列,那么您似乎更有可能需要将其排除在列列表之外,并且不要在其中传递任何明确的值。即

INSERT INTO W711_Reservation_Data
   (Network_ID, EventTitle, StartDate, EndDate, Justification) 
Values
  (@networkID2, @eventTitle1, @startDate1, @endDate1, @justification1)

if your id is an identity (aka auto generated from the database), just do not list the ID field in any place in the INSERT Statement, not as column name and not in the values list: 如果您的ID是一个标识(又名是从数据库自动生成的),则不要在INSERT语句的任何位置列出ID字段,而不是列名和值列表中:

to get the ID generated by SQL Server you call SCOPE_IDENTITY in this way: 要获取由SQL Server生成的ID,您可以通过以下方式调用SCOPE_IDENTITY:

INSERT INTO W711_Reservation_Data(Network_ID, EventTitle, StartDate, EndDate, Justification) Values(@networkID2, @eventTitle1, @startDate1, @endDate1, @justification1)";

RETURN SCOPE_IDENTITY()

Edit: this is the second of your two statements, I have removed the ID and the NULL... 编辑:这是您的两个语句中的第二个,我已经删除了ID和NULL ...

There might be two possibility. 可能有两种可能性。

1] if Network_ID in first table is primary key auto generated then insert data in first table. 1]如果第一个表中的Network_ID是自动生成的主键,则在第一个表中插入数据。 then get latest network id from that table and pass that network id with second query. 然后从该表中获取最新的网络ID,并通过第二个查询传递该网络ID。

2]. 2]。 If ID column in second table is primary key then Do not pass null in second query. 如果第二个表中的ID列是主键,则不要在第二个查询中传递null。 either make auto generated or pass uniquer value in query. 使自动生成或在查询中传递唯一值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM