How to get the primary key of the last row inserted into the table

人走茶凉 提交于 2019-11-28 14:28:53

A separate SQL statement

SELECT SCOPE_IDENTITY()

Or the OUTPUT clause

INSERT MyTable (...=
OUTPUT INSERTED.KeyCol
VALUES (...) --Or SELECT ... FROM Another table)

Edit:

  • change your insert statement to match mine
  • change .ExecuteNonQuery() to blah = xxx.ExecuteScalar()

As part of your INSERT statement (into the first News table), after it runs, execute

SELECT SCOPE_IDENTITY()

and return NewsId value to caller.

Ref.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!