gpt4 book ai didi

c# - 表没有得到更新,当使用 executeNonquery

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:00 25 4
gpt4 key购买 nike

我正在尝试更改用户密码。我无法更新密码 :(。我收到的消息是密码已更改,因为它没有更改。。我的代码如下。如果有人可以建议我哪里出错了,请。我只是一个初学者...

protected void Button1_Click(object sender, EventArgs e)
{
DatabaseLayer data = new DatabaseLayer();

string username = Session["Authenticate"].ToString();
string password = TextBox1.Text;
string newpass = TextBox2.Text;
string confirm = TextBox3.Text;
string flag = "";

if (newpass.ToString() == confirm.ToString())
{
flag = data.passwordChange(username, password, newpass);
Literal1.Text = flag.ToString();
}
else
{
Literal1.Text = "New Password does not match the Confirm Password ";
}
}

上面的点击事件必须修改我的密码,函数passwordChange如下..

public string passwordChange(string username, string password, string newPasswd)
{
string SQLQuery = "SELECT password FROM LoginAccount WHERE username = '" + username + "'";
string SQLQuery1 = "UPDATE LoginAccount SET password = ' " + newPasswd + " ' WHERE username = ' " + username + "'";
SqlCommand command = new SqlCommand(SQLQuery, sqlConnection);
SqlCommand command1 = new SqlCommand(SQLQuery1, sqlConnection);

sqlConnection.Open();
string sqlPassword = "";
SqlDataReader reader;

try
{
reader = command.ExecuteReader();


if (reader.Read())
{
if (!reader.IsDBNull(0))
{
sqlPassword = reader["password"].ToString();
}
}
reader.Close();

if (sqlPassword.ToString() == password.ToString())
{
try
{
int flag = 0;
flag = command1.ExecuteNonQuery();

if (flag > 0)
{
sqlConnection.Close();
return "Password Changed Successfully";
}
else
{
sqlConnection.Close();
return "User Password could not be changed";
}
}
catch (Exception exr)
{
sqlConnection.Close();
return "Password Could Not Be Changed Please Try Again";
}
}
else
{
sqlConnection.Close();
return "User Password does not Match";
}
}
catch (Exception exr)
{
sqlConnection.Close();
return "User's Password already exists";
}
}

我在附近放了一个断点

if(flag>0)

它仍然显示 executeNonquery 没有返回更新的行值,而且在 SQL Server 的后端,它没有改变,如果有人可以纠正我,请...我应该使用其他执行命令还是什么?我正在使用 VS 2008 和 SQL Server 2005 执行此操作..

最佳答案

1:这是单引号和双引号之间的间距:(如:' "+ username + "')
2) 你在乞求 SQL 注入(inject)。

在您的 PasswordChange 方法中试试这个:

public string PasswordChange(string userName, string oldPass, string newPass)
{
using(SqlConnection sqlConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["LoginDb"].ConnectionString))
{
string sqlToConfirmOldPass =
"SELECT password FROM LoginAccount WHERE username = @userName";
string sqlToUpdatePassword =
"UPDATE LoginAccount SET password = @newPass WHERE username = @userName";

SqlCommand confirmOldPass = new SqlCommand(sqlToConfirmOldPass, sqlConnection);
confirmOldPass.Parameters.AddWithValue("@userName", userName);

SqlCommand updatePassword = new SqlCommand(sqlToUpdatePassword, sqlConnection);
updatePassword.Parameters.AddWithValue("@newPass", newPass);
updatePassword.Parameters.AddWithValue("@userName", userName);

[Rest of your code goes here]
}
}

我也没有看到您在哪里设置 SqlConnection,所以我为此添加了一行。您需要根据需要对其进行修改。

关于c# - 表没有得到更新,当使用 executeNonquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3503427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com