gpt4 book ai didi

c# - web 方法不更新 sql server 上的详细信息

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:16 24 4
gpt4 key购买 nike

我正在尝试在学生登录后更新他们的详细信息。我希望能够在他们单击按钮时更新他们的密码和电子邮件地址。它的

[WebMethod]
public bool EditAccount(Student student)
{
bool UploadSuccess = false;
cn.Open();
SqlCommand com = new SqlCommand("UPDATE tblStudent set EmailAddress='" + student.EmailAddress + "', Password='" + student.Password + "'", cn);
{
com.Parameters.AddWithValue("@EmailAddress", student.EmailAddress);
com.Parameters.AddWithValue("@Password", student.Password);
int i = com.ExecuteNonQuery();
cn.Close();
if (i != 0)
UploadSuccess = true;
return UploadSuccess;
}
}

protected void btnSave_Click(object sender, EventArgs e)
{
Student edit = stu.EditAccount();
edit.EmailAddress = txtEmail.Text;
edit.Password = txtPassword2.Text;

if (stu.EditAccount(edit))
{
Response.Write("<script type=\"text/javascript\">alert('Successful');</script>");
}
else
{
Response.Write("<script type=\"text/javascript\">alert('Password not matched');</script>");
}
}

最佳答案

您的代码似乎很乱,请尝试像这样重写它。同时 ExecuteNonQuery 返回 受影响的行数。如果您尝试返回某些内容,请尝试使用 ExecuteScalarReader

bool UploadSuccess = false;
int i;
cn.Open();
SqlCommand com = new SqlCommand("UPDATE tblStudent set EmailAddress=@EmailAddress, Password=@Password", cn);
{
com.Parameters.AddWithValue("@EmailAddress", student.EmailAddress);
com.Parameters.AddWithValue("@Password", student.Password);
try
{
i=com.ExecuteNonQuery();
}
catch(Exception e)
{
throw e;
}
cn.Close();
if (i != 0)
UploadSuccess = true;
return UploadSuccess;
}

关于c# - web 方法不更新 sql server 上的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22531769/

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