简体   繁体   中英

System.Data.SqlClient.SqlException: Incorrect syntax near '='

I am trying to validate login form using a SqlDataReader , but I get a syntax error

System.Data.SqlClient.SqlException: Incorrect syntax near '='

I have reviewed my code and don't seem to detect the above error. Please assist.

This is my code:

string btnString = "SELECT userName, passWord, FacultyId, StudentId FROM LOGIN";
btnString += "WHERE(userName=@name) AND (passWord=@word)";

SqlCommand cc = new SqlCommand();
SqlDataReader sr;

cc.Connection = sqlConn;
cc.CommandType = CommandType.Text;
cc.CommandText = btnString;

cc.Parameters.Add("@name", SqlDbType.Char).Value = txtUserName.Text;
cc.Parameters.Add("@word", SqlDbType.Char, 8).Value = txtPassWord.Text;

sr = cc.ExecuteReader();

if (sr.HasRows == true)
{
    Response.Write("<script>alert('Login is successful!')</script>");
}

The error is displayed at

sr = cc.ExecuteReader();

你需要一个空间

 btnString += " WHERE(userName=@name) AND (passWord=@word)";

Your missing a space in the commandtext.

string btnString = "SELECT userName, passWord, FacultyId, StudentId FROM LOGIN";
btnString += " WHERE(userName=@name) AND (passWord=@word)";

Should do it

A space is missing. You can use multiline strings instead of concatenation to avoid such problems in future (note @ before the " ):

string btnString = @"
    SELECT userName, passWord, FacultyId, StudentId FROM LOGIN
    WHERE(userName=@name) AND (passWord=@word)";

You can read more about strings here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM