c#中的update语句,sql
本文关键字:sql 语句 update 中的 | 更新日期: 2023-09-27 18:17:39
从我从textbox1,textbox2得到的输入,我想检查与DB,如果给定的用户名和密码组合是可用的,那么我想更新密码与输入可用的textbox3。以下是我的代码,但似乎我的更新语句不能正常工作,请帮助我纠正它…
protected void Button1_Click(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection("server=localhost; database=e-learningsystem; uid=root; password=123;port=3307;");
connection.Open();
try
{
MySqlCommand cmd1 = new MySqlCommand("UPDATE lecturer Set Password= '" + TextBox3.Text + "' WHERE UserName='" + TextBox1.Text + "' AND Password='" + TextBox2.Text + "'", connection);
cmd1.ExecuteNonQuery();
if (cmd1.ExecuteNonQuery() > 0)
{
Response.Write(@"<script language='javascript'>alert('Your Password Has Been Changed successfully!, Congratulations!')</script>");
}
else
{
Response.Write(@"<script language='javascript'>alert('Your Password Has Not Been Changed successfully!, Sorry!')</script>");
}
connection.Close();
}
catch (Exception ex)
{
Response.Write(@"<script language='javascript'>alert(ex.Message)</script>");
}
}
您试图通过两次调用ExecuteNonQuery
来更新密码。在第一次调用ExecuteNonQuery
时,它将返回未收集的1
,当第二次调用ExecuteNonQuery
时,它将返回0
,因此它将始终执行else
部分。
做以下修改-
int updateCount = cmd1.ExecuteNonQuery();
if (updateCount > 0)
{
Response.Write(@"<script language='javascript'>alert('Your Password Has Been Changed successfully!, Congratulations!')</script>");
}
else
{
Response.Write(@"<script language='javascript'>alert('Your Password Has Not Been Changed successfully!, Sorry!')</script>");
}