SQL错误:关键字“用户”附近的不正确语法

时间:2022-01-16 00:39:35

I am using SQL to insert data to SQL Database file using C# as follows.

使用c#将数据插入到SQL数据库文件中,如下所示。

    String cs = System.Configuration.ConfigurationManager.ConnectionStrings["connection1"].ConnectionString;
    SqlConnection conn = new SqlConnection(cs);
    String sql = "INSERT INTO User (login, password, status) " + 
            "VALUES (@login, @password, @status)";
    SqlCommand comm = new SqlCommand(sql, conn);

    comm.Parameters.Add("@login", System.Data.SqlDbType.VarChar);
    comm.Parameters.Add("@password", System.Data.SqlDbType.VarChar);
    comm.Parameters.Add("@status", System.Data.SqlDbType.Bit);

    try
    {
        conn.Open();
        Console.WriteLine(conn.ToString());
        comm.ExecuteNonQuery();
        conn.Close();
        return true;
    }
    catch (Exception ex)
    {
        throw (ex);
    }
    finally
    {
        conn.Close();
    }

I am getting the following error when command is executing.

当命令执行时,我将得到以下错误。

Incorrect syntax near the keyword 'User'.: INSERT INTO User (login, password, status) VALUES (@login, @password, @status)

不正确的语法接近关键字“用户”。:插入用户(登录、密码、状态)值(@login、@password、@status)

How can I solve this please?

我怎么解决这个问题?

edit: missed parameter values added..

编辑:添加的参数值被忽略。

    comm.Parameters["@login"].Value = this.Username;
    comm.Parameters["@password"].Value = this._password;
    comm.Parameters["@status"].Value = this.Status;

3 个解决方案

#1


66  

User is a reserved keyword, so you must use square brackets to make it explicit that you mean the object named "User" it, i.e. use [User] instead of User.

用户是一个保留的关键字,所以您必须使用方括号来明确表示您的意思是“用户”的对象,即使用[用户]而不是用户。

#2


5  

User is a t-sql reserved keyword. Enclosing it in square brackets should solve this. E.g INSERT INTO [User]

User是t-sql保留关键字。将它括在方括号中应该可以解决这个问题。E。g插入[用户]

#3


3  

run your query against the database. You can use the declare sql keyword to define your variables and give them values. If you need to figure out the variables values, set a breakpoint at conn.Open and then use the locals window to see what values you are passing in. Another tool at your disposal is the Sql Profiler. You can start a trace then run your program. You should be able to see the query as executed in the profile after the code you have posted has run.

对数据库运行查询。您可以使用declare sql关键字来定义变量并给它们赋值。如果需要计算变量值,请在conn.Open中设置断点,然后使用local窗口查看传入的值。您可以使用的另一个工具是Sql分析器。您可以启动跟踪,然后运行您的程序。您应该能够看到在您所发布的代码运行后在概要文件中执行的查询。

All of this should help you to figure out what is wrong with your sql when the exception does not provide enough information.

所有这些都应该帮助您找出当异常没有提供足够的信息时,您的sql有什么问题。

The Sql Server Management Studio should have highlighted the User keyword in your sql statement, easily showing that you need brackets around it like so: [User]

Sql Server Management Studio应该已经在Sql语句中突出显示了User关键字,这很容易表明您需要在它周围加上括号,比如:[User]

#1


66  

User is a reserved keyword, so you must use square brackets to make it explicit that you mean the object named "User" it, i.e. use [User] instead of User.

用户是一个保留的关键字,所以您必须使用方括号来明确表示您的意思是“用户”的对象,即使用[用户]而不是用户。

#2


5  

User is a t-sql reserved keyword. Enclosing it in square brackets should solve this. E.g INSERT INTO [User]

User是t-sql保留关键字。将它括在方括号中应该可以解决这个问题。E。g插入[用户]

#3


3  

run your query against the database. You can use the declare sql keyword to define your variables and give them values. If you need to figure out the variables values, set a breakpoint at conn.Open and then use the locals window to see what values you are passing in. Another tool at your disposal is the Sql Profiler. You can start a trace then run your program. You should be able to see the query as executed in the profile after the code you have posted has run.

对数据库运行查询。您可以使用declare sql关键字来定义变量并给它们赋值。如果需要计算变量值,请在conn.Open中设置断点,然后使用local窗口查看传入的值。您可以使用的另一个工具是Sql分析器。您可以启动跟踪,然后运行您的程序。您应该能够看到在您所发布的代码运行后在概要文件中执行的查询。

All of this should help you to figure out what is wrong with your sql when the exception does not provide enough information.

所有这些都应该帮助您找出当异常没有提供足够的信息时,您的sql有什么问题。

The Sql Server Management Studio should have highlighted the User keyword in your sql statement, easily showing that you need brackets around it like so: [User]

Sql Server Management Studio应该已经在Sql语句中突出显示了User关键字,这很容易表明您需要在它周围加上括号,比如:[User]