Syntax Error with Database Login System

六月ゝ 毕业季﹏ 提交于 2019-12-02 07:37:42

That happens because USER is a reserved keyword. To refer to it in your queries you need to encapsulate the word between square brackets

 string query = string.Format("SELECT COUNT(*) FROM [user] WHERE name = .....

but at this point, why in subsequent queries you use the name users? It is just a typo in the first query or in the next?

However, you should also keep in mind that string formatting your queries in that way is very bad.
You are at risk of Sql Injections and, if a single quote appears in your text values, the whole query will resul in an invalid syntax

As an example of a parameterized query to replace your query

 string query = "SELECT COUNT(*) FROM [user] WHERE name = @name",
 command.CommandText = query;
 command.Parameters.AddWithValue("@name",login);
 int amountOfUsers = (Convert.ToInt32(command.ExecuteScalar());
 if(amountOfUsers > 0)
 {
    .....
 }

Then the next problem is the password retrieved from the database and compared with the user input. Having this kind of code means the password is stored in clear text inside the database. A clear security risk. You should store passwords in an encrypted form. When you need to check the user credentials you apply the same crypting algorithm to the user input and checks against the password in the database.

You could do this in a single operation

 string cryptPwd = EncryptPassword(password);
 string query = "SELECT COUNT(*) FROM [user] WHERE name = @name and password = @cryptpwd",
 ....
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!