如果不存在,如何将datagridview行插入数据库,但是如果存在,则更新,然后每行具有相同的ID?

时间:2019-03-05 15:56:52

标签: c# sql winforms datagridview

我的项目中有datagridview,其中有列productID,产品名称,数量,价格,折扣和行价。

插入工作正常:

private void addProducts()
    {
        string queryInvoice = @"INSERT INTO order_lines (orderID, productID, quantity, discount) VALUES (@orderID, @productID, @quantity, @discount)";

        /*
        string queryInvoice = @"IF EXISTS(SELECT * FROM order_lines WHERE orderID = @orderID)
                                UPDATE order_lines
                                SET productID = @productID,
                                    quantity = @quantity,
                                    discount = @discount
                                ELSE
                                    INSERT INTO order_lines(orderID, productID, quantity, discount) VALUES (@orderID, @productID, @quantity, @discount);";

        */
        con = new SqlConnection(connectionString);
        try
        {
            using (SqlCommand cmd = new SqlCommand(queryInvoice, con))
            {
                con.Open();
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    cmd.Parameters.AddWithValue("@orderID", InvoiceNumber);
                    cmd.Parameters.AddWithValue("@productID", dataGridView1.Rows[i].Cells["ProductCode"].Value);
                    cmd.Parameters.AddWithValue("@quantity", Convert.ToDouble(dataGridView1.Rows[i].Cells["productQuantity"].Value));
                    cmd.Parameters.AddWithValue("@discount", dataGridView1.Rows[i].Cells["productDiscount"].Value);

                    cmd.ExecuteNonQuery();

                    cmd.Parameters.Clear();
                } // end loop
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            MessageBox.Show("Inserted!");
        }
    }

插入后,它在数据库中看起来像这样:Database values

如您所见,我也尝试过如果数据库中存在该库,则更新else插入,但是它不起作用。当它添加第一行时,它将更新然后添加的那一行,因此我只能看到数据库中添加了一行(在datagridview中为最后一行)。

我通过其他形式将值发送到了datagridview:

private void productValues()
    {
        con = new SqlConnection(conString);
        try
        {
            con.Open();
            adapter = new SqlDataAdapter("SELECT ol.productID AS 'Tuotekoodi', p.name AS 'Nimike',ol.quantity AS 'Määrä', p.price AS 'á hinta', ol.discount AS 'Alennusprosentti', CAST(((p.price * ol.quantity) * (ol.discount/100)) AS decimal(16,2)) AS 'Total' FROM order_lines AS ol LEFT JOIN product AS p ON ol.productID = p.productID WHERE ol.orderID = @orderID", con);
            adapter.SelectCommand.Parameters.AddWithValue("@orderID", dgv_invoices.CurrentRow.Cells[0].FormattedValue.ToString());
            cmdbl = new SqlCommandBuilder(adapter);
            dt = new DataTable();
            adapter.Fill(dt);
            DataRow dataRow = dt.NewRow();
            dt.Rows.Add(dataRow);
            InvoicePage.dataGridView1.DataSource = dt;

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            con.Close();          
        }
    }

但是我在更新产品编号,数量和折扣时遇到问题。

那么我该如何解决该问题?

0 个答案:

没有答案