访问数据库错误:“在UPDATE语句中出现语法错误。”在c#中

时间:2013-09-11 11:30:03

标签: c# winforms ms-access datagridview

我有一个datagridview。我从数据库中获取数据并在此gridview中显示。但是,当我尝试通过单击按钮来更新它时,它会显示错误。

            try
            {
                string update = "update Tank_Head set Item_Code='?', Opening_Bal='?', Tank_Description='?', where Tank_Unit='?' and companyID='?'";
                OleDbCommand cmd = new OleDbCommand(update, con);
                cmd.Parameters.AddWithValue("@Item_Code", cbItemEdit.Text);
                cmd.Parameters.AddWithValue("@Opening_Bal", txtOpeningBalanceEdit.Text);
                cmd.Parameters.AddWithValue("@Tank_Description", txtTankDesEdit.Text);
                cmd.Parameters.AddWithValue("@Tank_Unit", txtTankUnitEdit.Text);
                cmd.Parameters.AddWithValue("@companyID", label1.Text);
                i = cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                con.Close();
                if (i != 0)
                {
                    pnlView.Visible = true;
                    pnlEdit.Visible = false;
                }
            }

3 个答案:

答案 0 :(得分:1)

你那里有一个流浪逗号:

update Tank_Head set Item_Code='?', Opening_Bal='?', Tank_Description='?', <<< where Tank_Unit='?' and companyID='?'

删除它,它应该可以正常工作:

update Tank_Head set Item_Code='?', Opening_Bal='?', Tank_Description='?' where Tank_Unit='?' and companyID='?'

答案 1 :(得分:1)

,

之前,您的查询中还有where

替换你的查询

string update = "update Tank_Head set Item_Code='?', Opening_Bal='?', Tank_Description='?'  where Tank_Unit='?' and companyID='?'";

答案 2 :(得分:0)

在更新最后一个字段后出现错误导致错误。尝试将语句更改为

string update = "update Tank_Head set Item_Code='?', Opening_Bal='?', Tank_Description='?' where Tank_Unit='?' and companyID='?'";