Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表名附近的C#语法错误_C#_Sql_Database_Select - Fatal编程技术网

表名附近的C#语法错误

表名附近的C#语法错误,c#,sql,database,select,C#,Sql,Database,Select,我在尝试检查C#中三层项目的登录凭据时遇到一些问题 目前,我有一个名为User的表,其中包含userName和password列 在我的BusinessLogicLayer中,我获取用户输入并将其传递给dataAccessLayer: public string checkCredential(string userName, string password) { string returnMessage = ""; User user = new User(userName,

我在尝试检查C#中三层项目的登录凭据时遇到一些问题

目前,我有一个名为User的表,其中包含userName和password列

在我的BusinessLogicLayer中,我获取用户输入并将其传递给dataAccessLayer:

public string checkCredential(string userName, string password)
{
    string returnMessage = "";
    User user = new User(userName, password);
    Boolean success = user.checkCredential();
    if (!success)
    {
        returnMessage += "Username and password does not match!";
    }
    else
    {
        returnMessage = "";
    }
    return returnMessage;
}
在我的数据访问层中,我得到了一种检查登录凭据的方法:

public Boolean checkCredential()
{
    Boolean result = false;

    using (var connection = new SqlConnection(FFTHDb.connectionString)) // get your connection string from the other class here
    {
        SqlCommand command = new SqlCommand("SELECT userName, password FROM User WHERE userName = '" + userName + "' AND password = '" + password + "'", connection);
        connection.Open();
        using (var dr = command.ExecuteReader())
        {
            if (dr.Read())
            {
                result = true;
            }
        }
    }
    return result;
}
我得到了一个单独的类来设置连接字符串:

public static string connectionString = DataAccessLayer.Properties.Settings.Default.DBConnStr;


public static SqlDataReader executeReader(string query)
{
    SqlDataReader result = null;

    System.Diagnostics.Debug.WriteLine("FFTHDb executeReader: " + query);

    SqlConnection connection = new SqlConnection(connectionString);
    SqlCommand command = new SqlCommand(query, connection);
    connection.Open();
    result = command.ExecuteReader();
    connection.Close();

    return result;
}
没有编译错误。我仔细检查了数据库中的表名和列。然而,它总是告诉我用户附近有语法错误。我不知道为什么会这样


提前感谢。

问题:您提供的表名是
User
Transact-SQL
中的关键字

解决方案:将保留字
User
括在方括号中
[]

解决方案1:

SqlCommand command = new SqlCommand("SELECT userName, password FROM [User] WHERE userName = '" + userName + "' AND password = '" + password + "'", connection);
解决方案2:

    SqlCommand command = new SqlCommand("SELECT userName, password FROM [User] WHERE userName= @username AND password = @password", connection);
    command.Parameters.AddWithValue("@username",userName);
    command.Parameters.AddWithValue("@password",password);

用户是一个保留关键字,因此需要在其周围添加方括号。有关列表,请参阅。 所以,你应该这样做

SELECT userName, password FROM [User] WHERE userName =
用户
是T-SQL上的用户。您应该使用方括号,如
[User]

也总是使用一个好的实践


而且永远不要以纯文本形式存储密码使用。

有效。但是为什么我们需要加上方括号呢?解释你的答案。我已经知道了。在asp.net中,我们总是需要将表名用[]括起来,这是真的吗?@Downvoter如果您能改进一下,会很高兴的。@Ashburaczenko:请检查我的解释。