让用户更改自己的密码
用户更改自己的密码,不必在asp.net前先去判断输入的旧密码是否正确,可以一次性把旧密码与新密码传入存储过程,然后在存储过程中去判断。
表[Users]结构,可参考:http://www.cnblogs.com/insus/articles/1932670.html
存储过程:
代码
CREATE PROCEDURE [dbo].[usp_Users_UpdatePassword]
(
--存储过程,传入三个参数,记录ID,旧密码和新密码
@ID INT,
@OldPassword nvarchar(30),
@Password nvarchar(30)
)
AS
IF NOT EXISTS(SELECT TOP 1 * FROM [Users] WHERE [Password] = @OldPassword AND [UsersId] = @ID)
BEGIN
RAISERROR(N'旧密码输入不正确,无法更新!',16,1)
RETURN
END
BEGIN TRANSACTION
DECLARE @err int
--更新用户密码
UPDATE [Users] SET [Password] = @Password WHERE [UsersId] = @ID
SET @err = @@ERROR
IF @err <> 0
BEGIN
ROLLBACK TRANSACTION
END
COMMIT TRANSACTION
(
--存储过程,传入三个参数,记录ID,旧密码和新密码
@ID INT,
@OldPassword nvarchar(30),
@Password nvarchar(30)
)
AS
IF NOT EXISTS(SELECT TOP 1 * FROM [Users] WHERE [Password] = @OldPassword AND [UsersId] = @ID)
BEGIN
RAISERROR(N'旧密码输入不正确,无法更新!',16,1)
RETURN
END
BEGIN TRANSACTION
DECLARE @err int
--更新用户密码
UPDATE [Users] SET [Password] = @Password WHERE [UsersId] = @ID
SET @err = @@ERROR
IF @err <> 0
BEGIN
ROLLBACK TRANSACTION
END
COMMIT TRANSACTION