我是想要通过在文本框中输入电子邮件地址和新密码来更改任何人密码的管理员。存储过程如下:
Alter proc spChangePassword
@Email varchar(100),
@Passwordd varchar(100)
as
begin
IF EXISTS (SELECT * FROM tblRegister WHERE Email=@Email)
begin
UPDATE tblRegister
SET Passwordd=@Passwordd where Email=@Email
Select 0
end
ELSE
BEGIN
Select -1
end
end
,代码隐藏如下:
private void ChangePassword()
{
string CS = ConfigurationManager.ConnectionStrings["ABCD"].ConnectionString;
using(SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("spChangePassword", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Email",txtEmail.Text);
cmd.Parameters.AddWithValue("@Passwordd", txtPassword.Text);
cmd.ExecuteNonQuery();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
ChangePassword();
}
但我想用if和else语句检查表中是否存在电子邮件。如果电子邮件存在,则更改密码,否则抛出异常。我该怎么办?
答案 0 :(得分:0)
Begin
DECLARE @id AS INT
SELECT @id = tblRegisterId FROM tblRegisterWHERE Email =@Email
IF @id IS not NULL
Begin
UPDATE tblRegister
SET Passwordd=@Passwordd where Email=@Email
Select 1
End
Else
Begin
Select 0
End
End
答案 1 :(得分:0)
您可以简单地更改过程的代码并让它返回一个值。 0表示密码已更新,-1表示没有提供的电子邮件。
ALTER proc spChangePassword
@Email varchar(100),
@Passwordd varchar(100)
AS
BEGIN
IF EXISTS (SELECT * FROM Users WHERE Email=@Email) THEN
BEGIN
UPDATE tblRegister
SET Passwordd=@Passwordd where Email=@Email
SELECT 0 AS Result
END
ELSE
BEGIN
SELECT -1 AS Result
END
END
然后你必须阅读存储过程的结果并相应地采取行动。因此,您的服务器端代码必须更改为以下内容:
var reader = cmd.ExecuteReader();
while (reader.Read())
{
if(int.Parse(reader["Result"].ToString())==0)
{
// success
}
else
{
// failure
}
};
更新在if
声明中,您也可以使用此声明:
Convert.ToInt32(reader["Result"])==0
我认为它会像魅力一样起作用。
答案 2 :(得分:0)
试试这个: -
private bool ChangePassword()
{
string CS = ConfigurationManager.ConnectionStrings["ABCD"].ConnectionString;
using(SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("spChangePassword", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Email",txtEmail.Text);
cmd.Parameters.AddWithValue("@Passwordd", txtPassword.Text);
int count = cmd.ExecuteNonQuery();
if (count > 0)
return true;
else
return false;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
bool success = ChangePassword(); //Use this success variable to show a message.
}
您也可以更改存储过程,但不会抛出任何异常,只会检查。如果电子邮件存在,它将执行更新查询: -
Create proc spChangePassword
@Email varchar(100),
@Passwordd varchar(100)
as
begin
IF EXISTS ( SELECT * FROM tblRegister WHERE Email = @Email)
BEGIN
UPDATE tblRegister
SET Passwordd=@Passwordd where Email=@Email
END
end
答案 3 :(得分:0)
尝试此存储过程(请使用 IF EXISTS )
CREATE PROCEDURE InsertName
(
@Email varchar(25),
@Passwordd varchar(25)
)
AS
IF EXISTS(SELECT 'True' FROM tblRegister WHERE Email = @Email)
BEGIN
--This means it exists,update
UPDATE tblRegister
SET Passwordd=@Passwordd where Email=@Email
SELECT 'Changed successfully'
END
ELSE
BEGIN
--This means the record isn't in there already
SELECT 'Does Not Exist'
END
private string ChangePassword()
{
string CS = ConfigurationManager.ConnectionStrings["ABCD"].ConnectionString;
using(SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("spChangePassword", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Email",txtEmail.Text);
cmd.Parameters.AddWithValue("@Passwordd", txtPassword.Text);
return cmd.ExecuteNonQuery().ToString();
}
}
答案 4 :(得分:0)
SQL
Create proc spChangePassword
@Email varchar(100),
@Passwordd varchar(100)
as
begin
IF EXISTS ( SELECT * FROM tblRegister WHERE Email = @Email)
BEGIN
UPDATE tblRegister
SET Passwordd=@Passwordd where Email=@Email
END
ELSE
BEGIN
RAISEERROR('Email does not exists',0,1)
END
end
C#
string CS = ConfigurationManager.ConnectionStrings["ABCD"].ConnectionString;
using(SqlConnection con = new SqlConnection(CS))
{
try{
con.Open();
SqlCommand cmd = new SqlCommand("spChangePassword", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Email",txtEmail.Text);
cmd.Parameters.AddWithValue("@Passwordd", txtPassword.Text);
cmd.ExecuteNonQuery();
}
catch(SqlException ee)
{
...
}
}