update person set password='" + Password1.Value + "' where name='" + Text1.Value + "'

来源:百度知道 编辑:UC知道 时间:2024/05/28 09:07:17
请高手帮忙,上面的语句有错吗?
string strsql = "update person set password='" + Password1.Value + "' where name='" + Text1.Value + "'";
SqlCommand cm= new SqlCommand(strsql, conn);
cm.Connection.Open();
SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
cm.ExecuteNonQuery();
Response.Redirect("company.aspx");
}
else
{ Response.Write("<script language='javascript'>alert('对不起,该用户名不存在!')</script>"); }
dr.Close();
cm.Connection.Close();
虽然数据可以添加到数据库中,可是不执行”Response.Redirect("company.aspx");”而执行 "Response.Write("<script language='javascript'>alert('对不起,该用户名不存在!')</script>"); " 请高手指教!!!

看起来没什么问题,跟空格没关系,前面已经有单引号括起来了。
如果报了错误你把错误和代码贴出来瞧瞧
------------------------------------------------------
你这个和Sql没有关系,也没有必要用DataReader了.
直接 cm.ExecuteNonQuery(); 返回影响行数,如果影响行数为0说明不存在这个用户名,否则操作成功.
if(cm.ExecuteNonQuery()>0)
{
Response.Redirect("company.aspx");
}
else
{
Response.Write("<script language='javascript'>alert('对不起,该用户名不存在!')</script>");
}

where 前面少个空格?

update person set password='" + Password1.Value + "' where name='" + Text1.Value + "'