C# ASP.NET tableadapter插入未返回正确的标识值

C# ASP.NET tableadapter插入未返回正确的标识值,c#,asp.net,sql-server,C#,Asp.net,Sql Server,在我的VisualStudio2008 ASP.NET网页codebehind中,我在SQL tableadapter中调用一个add例程,如下所示 NewLabID = LabItem.AddLaborItem(ThisWONum, TechID, DateTime.Now, DateTime.Now, "Working", ""); 正在正确添加记录,但“NewLabID”每次返回1,而不是实际的新LabID(LabID定义为int/identity)。自动生成的代码插入命令是 INSER

在我的VisualStudio2008 ASP.NET网页codebehind中,我在SQL tableadapter中调用一个add例程,如下所示

NewLabID = LabItem.AddLaborItem(ThisWONum, TechID, DateTime.Now, DateTime.Now, "Working", "");
正在正确添加记录,但“NewLabID”每次返回1,而不是实际的新LabID(LabID定义为int/identity)。自动生成的代码插入命令是

INSERT INTO [LaborDetail] ([WONum], [TechID], [StartDate], [StopDate], [OtherType], [OtherDesc])
VALUES (@WONum, @TechID, @StartDate, @StopDate, @OtherType, @OtherDesc);
SELECT LabID FROM LaborDetail WHERE (LabID = SCOPE_IDENTITY())
调用此函数的自动生成代码是:

returnValue = command.ExecuteNonQuery();

使用调试器逐步执行此操作,returnValue始终为1。上面的命令不应该返回新的标识字段值吗?

ExecuteNonQuery返回受影响的行数。您将希望改用ExecuteScalar()。

谢谢-但是如何让asp.net自动生成ExecuteScalar而不是ExecuteOnQuery?自动生成的命令显然是为了返回新ID而设计的。好的,我得到了它-我必须在表适配器中更改查询的ExecuteMode属性。