System.Data.SqlClient.SqlException: '=' 附近有语法错误。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnChangePassword_Click(object sender, EventArgs e)
{
if (tbNewPassword.Text == tbReNewPassword.Text)
{
string sql = " Select*From Users Where Username = '" + Session["UserName"] + "'and password = '" + tbOldPassword.Text + "'";
DataTable dt = DbHelperSQL.Query(sql).Tables[0];
if (dt.Rows.Count > 0)
{
lblMess.Text = "ok";
sql = "Updata Users Set PassWord='" + tbNewPassword.Text + "' Where Username='" + Session["UserName"].ToString() + "'";
int n = DbHelperSQL.ExecuteSql(sql);
if (n > 0)
{
lblMess.Text = "修改成功";
lblMess.Text = "修改失败";
}
else
{
lblMess.Text = "两次输入密码不一样";
}
}
}

}
}
最新回答
长发与酒

2024-04-25 01:27:10

在代码处设置断点,然后启动调试,逐步执行。

或者用 SQL事件探查器,跟踪执行 的SQL脚本,查询脚本问题。
简单能看到的错误有2个。
Select*From ===> Select * From ; * 左右要空格
Session需要先进行判断,是否存在。
萌城少女

2024-04-25 02:31:03

这一句:string sql = " Select*From Users Where Username = '" + Session["UserName"] + "'and password = '" + tbOldPassword.Text + "'";
改成:
string sql = " Select * From Users Where Username =" + Session["UserName"] + " and password =" + tbOldPassword.Text;

这一句:sql = "Updata Users Set PassWord='" + tbNewPassword.Text + "' Where Username='" + Session["UserName"].ToString() + "'"; 也做类似的修改。