How to insert a record and return the newly created ID using a single SqlCommand?

前端 未结 9 1320
陌清茗
陌清茗 2020-12-02 18:18

I\'m using an SqlCommand object to insert a record into a table with an autogenerated primary key. How can I write the command text so that I get the newly created ID when I

9条回答
  •  甜味超标
    2020-12-02 19:00

    Don't use @@IDENTITY, however simple it may seem. It can return incorrect values.

    SELECT SCOPE_IDENTITY()
    

    appears to be the obvious choice.

提交回复
热议问题