从表适配器插入命令返回新的标识值

时间:2014-12-07 01:49:45

标签: asp.net sql-server insert identity

我在表适配器中有一个insert语句,但它一直返回1,而不是新插入的行的id。为什么呢?

Dim TA As New pTableAdapters.householdsTableAdapter
lblResult.Text = TA.Insert(New Guid("32dd20eb-1691-457b-9ff5-fc41d687e578"), Trim(Title), 0, "add", "addr2", _
            "4544", "london", "ld", 0, "", Date.Now)

表适配器中的insert语句:

INSERT INTO households (userid, title, side, address, address2, zipcode, city, state, country, notes, createdate) VALUES (@userid,@title,@side,@address,@address2,@zipcode,@city,@state,@country,@notes,@createdate);SELECT CAST(scope_identity() AS int)

1 个答案:

答案 0 :(得分:1)

从以下链接:http://blogs.msdn.com/b/smartclientdata/archive/2005/10/31/returnidentityvaluequery.aspx

  

...还有最后一个重要部分:在DataSet Designer中选择新查询,并将ExecuteMode属性从NonQuery更改为Scalar。 ...

NonQuery将返回受影响的行数(因此您总是得到1),而Scalar模式返回所选的值本身。