2015-02-10 46 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

相關問題