Return ID on INSERT?

后端 未结 5 908
灰色年华
灰色年华 2020-12-03 13:10

I have an INSERT query and I want the DB to return the ID of the row I just inserted.

sqlString = \"INSERT INTO MagicBoxes (OwnerID, Key, Name, Permissions,          


        
5条回答
  •  借酒劲吻你
    2020-12-03 13:44

    You have two options; you could declare an Output parameter called @ID; or - you could change the end to SELECT SCOPE_IDENTITY() and just use:

    int id = (int)cmd.ExecuteScalar();
    

    I prefer the formal parameter approach, but ExecuteScalar works well.

提交回复
热议问题