简体   繁体   中英

SQL Query incorrect syntax error c#

I'm getting the following error

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near the keyword 'Group'.

When I run the following query

string query = "Update Job Set Name = @Name, Date = @Date, Material = @Material, Instructions = @Instructions, Group = @Group, Time = @Time, Address = @Address Where Id = @Id";

DataRowView drv = (DataRowView)jobGrid.Items.GetItemAt(0);
string name = drv.Row[0].ToString();

int Id = getId(name);

using (connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
{
    connection.Open();
    string date = drv.Row[1].ToString();
    string material = drv.Row[2].ToString();
    string details = drv.Row[3].ToString();
    string group = drv.Row[4].ToString();
    string time = drv.Row[5].ToString();
    string address = drv.Row[6].ToString();

    command.Parameters.AddWithValue("@Name", name);
    command.Parameters.AddWithValue("@Date", date);
    command.Parameters.AddWithValue("@Material", material);
    command.Parameters.AddWithValue("@Instructions", details);
    command.Parameters.AddWithValue("@Group", group);
    command.Parameters.AddWithValue("@Time", time);
    command.Parameters.AddWithValue("@Address", address);
    command.Parameters.AddWithValue("@Id", Id);

    command.ExecuteNonQuery();
}

All of those things exist in the database in that order except for Id which comes first. I run a similar update using a different table in the database and it works perfectly. I'm not sure what is wrong with "Group" that makes that error. The value that I insert into Group is a string which is specified by the table ass varchar(50). I am using Visual Studio WPF c#

I can add and delete things from this table perfectly fine, but updating causes this issue

Use brackets for reserved keywords like Date,Group,Time etc

Update Job Set Name = @Name, [Date] = @Date, Material = @Material, Instructions = @Instructions, [Group] = @Group, [Time] = @Time, Address = @Address Where Id = @Id

check Reserved Keywords (Transact-SQL)

您必须在方括号中使用保留关键字尝试以下操作:更新作业集名称= @名称,[日期] = @日期,材料= @材料,指令= @指令,[组] = @组,[时间] = @时间,地址= @地址,其中ID = @Id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM