方法一:(未使用存储过程) 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))asDeclare @ID intbegin transactionInsert into sendmailbox(username) values(@UserName)Select @ID=@@IDENTITYcommit transactionreturn @ID