2015-02-10 48 views
1

我试图用SqlDataReader验证登录表单,但我得到一个语法错误System.Data.SqlClient.SqlException:附近有语法错误“=”

System.Data.SqlClient.SqlException:不正确的语法'='

我检查了我的代码,似乎没有检测到上述错误。请协助。

这是我的代码:

string btnString = "SELECT userName, passWord, FacultyId, StudentId FROM LOGIN"; 
btnString += "WHERE([email protected]) AND ([email protected])"; 

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>"); 
} 

sr = cc.ExecuteReader(); 

回答

7

显示错误你需要一个空间

btnString += " WHERE([email protected]) AND ([email protected])"; 
+0

谢谢,这个工作 – neds 2015-02-10 07:56:31

+0

不客气。检查@Konstantin Spirin答案中的多行字符串 – Mate 2015-02-10 08:02:27

1

你缺少在CommandText的空间。

string btnString = "SELECT userName, passWord, FacultyId, StudentId FROM LOGIN"; 
btnString += " WHERE([email protected]) AND ([email protected])"; 

应该这样做

1

的空间缺失。您可以使用多个字符串,而不是串联,以避免(在"前的注意事项@)在未来这样的问题:

string btnString = @" 
    SELECT userName, passWord, FacultyId, StudentId FROM LOGIN 
    WHERE([email protected]) AND ([email protected])"; 

你可以阅读更多有关字符串here

相关问题