Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL error: Incorrect syntax near the keyword 'User'

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

    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)

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; 
like image 422
BlueBird Avatar asked May 21 '11 14:05

BlueBird


People also ask

What is incorrect syntax near in SQL?

When executing a query in SQL and the editor throws back this error: Incorrect syntax near …'' That typically means you have used the wrong syntax for the query. This happens mostly when someone switched from one relational database to another relational database, from MySQL to MS SQL Server for example.

Is User keyword in SQL?

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 Answers

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.

like image 136
Marc Gravell Avatar answered Oct 02 '22 19:10

Marc Gravell


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

like image 36
Andy Johnson Avatar answered Oct 02 '22 18:10

Andy Johnson