Syntax error in update using C# inputting data into an existing row

谁说我不能喝 提交于 2020-01-25 06:57:26

问题


I am having problem with my code with the update query it shows the error syntax error in update statement. I would like to insert in data into an existing row with the columns already created.

private void save_care_Click(object sender, EventArgs e)
{
        if (textBox2.Text=="")
        {
            //Checking if workorder exist in database
            connection.Open();
            OleDbCommand checkrecord = new OleDbCommand("SELECT COUNT(*) FROM [c# barcode] WHERE ([Workorder] = @workorder)", connection);
            checkrecord.Parameters.AddWithValue("@workorder", textBox2.Text);

            int recordexist = (int)checkrecord.ExecuteScalar();

            if (recordexist > 0)
            {
                //add data if it exist
                string cmdText = "UPDATE [c# barcode] SET ([Close from care],[Name care]) VALUES (@Close, @name) WHERE ([Workorder] = @workorder)";

                using (OleDbCommand cmd = new OleDbCommand(cmdText, connection))
                {
                    cmd.Parameters.AddWithValue("@workorder", textBox2.Text);
                    cmd.Parameters.AddWithValue("@Close", DateTime.Now.ToString("d/M/yyyy"));
                    cmd.Parameters.AddWithValue("@name", label4.Text);

                    cmd.ExecuteNonQuery();

                    textBox2.Clear();
                    connection.Close();
                }

                connection.Close();
            }
            else
            {
                //inserting workorder if it does not exist
                string cmdText = "INSERT INTO [c# barcode] ([Workorder], [Close from care], [Name care]) VALUES (@workorder, @Close, @name)";

                using (OleDbCommand cmd = new OleDbCommand(cmdText, connection))
                {
                    cmd.Parameters.AddWithValue("@workorder", textBox2.Text);
                    cmd.Parameters.AddWithValue("@Close", DateTime.Now.ToString("d/M/yyyy"));
                    cmd.Parameters.AddWithValue("@name", label4.Text);

                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        textBox2.Clear();
                        MessageBox.Show("Insert successful, workorder has not been handed over, please check");
                    }
                    else
                    {
                        textBox2.Clear();
                        MessageBox.Show("Please rescan");
                        connection.Close();
                    }

                    connection.Close();
                }
            }
        }
        else
            MessageBox.Show("No data, Please scan workorder");
}

Error is at cmd.ExecuteNonQuery(); line.

For example the table in the picture under workorder there is a test4 the update will insert data into the column [Close from care] and [name care] in the test4 row


回答1:


I believe that you are using the INSERT syntax for an UPDATE. You should use the UPDATE syntax.

The example from Microsoft is as follows:

UPDATE Orders 
SET OrderAmount = OrderAmount * 1.1, 
    Freight = Freight * 1.03 
WHERE ShipCountry = 'UK';

So if we update yours to match, it should look like this:

UPDATE [c# barcode]
SET [Close from care] = @Close,
    [Name care] = @name
WHERE [Workorder] = @workorder

Obviously the newlines aren't important, it's just to make it easier to read.



来源:https://stackoverflow.com/questions/59149357/syntax-error-in-update-using-c-sharp-inputting-data-into-an-existing-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!