創建一個網站登錄系統,但是當我運行它時,它不斷提出這個錯誤到下面編碼的特定位。是否有人可以幫助我在這個特殊的代碼此錯誤消息:附近關鍵字 「使用者」語法錯誤:數據庫登錄系統的語法錯誤
System.Data.SqlClient.SqlException。
代碼:
public static User LoginUser(string login, string password)
{
//Check if user exists
string query = string.Format("SELECT COUNT(*) FROM TeesDB.dbo.user WHERE name = '{0}'",
login);
command.CommandText = query;
try
{
conn.Open();
int amountOfUsers = (int) command.ExecuteScalar();
if(amountOfUsers == 1)
{
//User exists, check if the password match
query = string.Format("SELECT password FROM users WHERE name = '{0}", login);
command.CommandText = query;
string dbPassword = command.ExecuteScalar().ToString();
if (dbPassword == password)
{
//password match. Login and password data are known to us.
//retrieve further user data from the database
query = string.Format("SELECT email, user_type FROM users WHERE name =
'{0}'", login);
command.CommandText = query;
SqlDataReader reader = command.ExecuteReader();
User user = null;
while (reader.Read())
{
string email = reader.GetString(0);
string type = reader.GetString(1);
user = new User(login, password, email, type);
}
return user;
}
else
{
//passwords do not match
return null;
}
}
else
{
//user exists
return null;
}
}
finally
{
conn.Close();
}
}
}
}
你的表很可能命名爲「用戶」而不是「用戶」做到這一點。另外,你應該參數化你的SQL命令,因爲你有很大的SQL注入風險。 [給我參數化SQL或給我死亡](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/)。另外,請看看像BCrypt鹽和散列你的密碼;不要以明文形式存儲它們! –
好的歡呼,所以你認爲如果我改變用戶的用戶,它應該工作,感謝您的反饋,我很欣賞它。即時通訊也非常新,所以即時通訊不知道你是什麼意思通過散列我的密碼 –
非常感謝你,我的部分輕微拼寫錯誤!感謝您的注意! –