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

后端 未结 2 1862
臣服心动
臣服心动 2020-12-12 03:14

I am using multiview index to insert the news details. In the first view the user can enter details of news which is then inserted into db on clicking the next button the se

相关标签:
2条回答
  • 2020-12-12 04:05

    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.

    0 讨论(0)
  • 2020-12-12 04:09

    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()
    0 讨论(0)
提交回复
热议问题