如何在数据库中插入一条记录后返回该条记录 uniqueidentifier 字段的值或该条记录的标识字段的值(即ID)

    技术2022-05-11  1

    方法一:(未使用存储过程)

    string  sqlc = " insert into sendmailbox (username) values (@Username) SELECT @@IDENTITY as id " ;SqlCommand myCommand  =   new  SqlCommand(sqlc, myConnection);myCommand.Parameters.Add( new  SqlParameter( " @Username " , SqlDbType.VarChar));myCommand.Parameters[ " @Username " ].Value  = un;myCommand.Connection.Open(); int  Id  =  Convert.ToInt32(myCommand.ExecuteScalar());myCommand1.Connection.Close();

    方法二:(用存储过程来实现)

    Create   procedure  insSendmail( @UserName   varchar ( 20 )) as Declare   @ID   int begin   transaction Insert   into  sendmailbox(username)  values ( @UserName ) Select   @ID = @@IDENTITY commit   transaction return   @ID  

    最新回复(0)