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

前端 未结 9 1318
陌清茗
陌清茗 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 18:50

    Immediately after your insert stmt, use

    SELECT CAST(scope_identity() AS bigint) ---- incase you have a return result as int64
    

    This will return the column created id/identity.

提交回复
热议问题