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

前端 未结 9 1289
陌清茗
陌清茗 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:38

    insert into Yourtable()  
    values()  
    SELECT SCOPE_IDENTITY()
    

    I just ran a test and verified that the semi-colons are optional using SQL Server 2005 SP2, and .Net 3.5

提交回复
热议问题