为什么在此代码中出现“您的SQL语法有错误”错误?

时间:2018-08-22 09:50:56

标签: c# mysql sql

我正在尝试使用更新查询,但是它显示以下语法错误:

  

“ MySql.Data.MySqlClient.MySqlException:'您的SQL语法有误;请查看与您的MariaDB服务器版本相对应的手册,以在'[Item Name] ='bulb'附近使用正确的语法,[数量类型] ='pcs',[数量] ='470',[商品价格(以卢比为单位)] ='在第1''

using (MySqlConnection connection = new MySqlConnection(con))
{
    try
    {
        connection.Open();
        using (MySqlCommand command = connection.CreateCommand())
        {
            command.CommandText = "UPDATE inventory Set [Item Name]=@itname,[Quantity Type]=@qtype,[Quantity]=@qty,[Item Price (in Rs.)]=@itprice,[Supplier]=@supl WHERE [pid]=@lpid";
            command.Parameters.AddWithValue("@lpid", lbl_dpid.Text);
            command.Parameters.AddWithValue("@itname", txtbox_itemname.Text);
            command.Parameters.AddWithValue("@qtype", cmbox_qtype.Text);
            command.Parameters.AddWithValue("@qty", txtbox_qty.Text);
            command.Parameters.AddWithValue("@itprice", txtbox_itprice.Text.ToString());
            command.Parameters.AddWithValue("@supl", txtbox_supplier.Text);

            DialogResult result = MessageBox.Show("Do You Want to Update?", "Update", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result.Equals(DialogResult.OK))
            {
                command.ExecuteNonQuery();
                connection.Close();
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

在数据库表中,pid的数据类型,项目价格(以卢比为单位)和数量的数据为int,其余为varchar。 This is what the design looks like

4 个答案:

答案 0 :(得分:8)

MySQL不使用方括号:

<image url = {`url(${this.url})`} />

它使用反引号:

UPDATE inventory Set [Item Name]=@itname

请记住,“ SQL语法错误”消息始终指向SQL解析器首先变得混乱的 exact 字符。它“近”的一开始。在这种情况下,该UPDATE inventory Set `Item Name`=@itname 字符。

答案 1 :(得分:1)

您需要解析int列的字符串:

command.Parameters.AddWithValue("@qty", int.Parse(txtbox_qty.Text));

答案 2 :(得分:0)

MySql不使用方括号。放在反引号

`Item Price (in Rs.)`

CommandText应该是这样

UPDATE inventory Set `Item Name`=@itname,`Quantity Type`=@qtype,`Quantity`=@qty,`Item Price (in Rs.)`=@itprice,`Supplier`=@supl WHERE `pid`=@lpid

答案 3 :(得分:0)

通过反斜线(`)替换[和]以下行: command.CommandText =“更新库存集[商品名称] = @ itname,[数量类型] = @ qtype,[数量] = @数量,[商品价格(以卢比为单位)] = @ itprice,[供应商] = @ supl在[pid] = @ lpid“;

相关问题