插入SQL语句不能按下按钮,但其他语句可以

时间:2016-03-16 02:59:35

标签: c# mysql sql

在下面的代码中,'Insert into Accounting'命令不起作用,但之前的Insert命令和之后的Update命令确实有效。在更新之前,程序的用户从数据网格中选择一行,并将数据网格中的值传输到文本框。

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        if (txtProdName.Text != "" && txtProdPrice.Text != "" && txtProdSupplier.Text != "")
        {
            if (MessageBox.Show("Update Product?", "Update Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                double PricePast = 0;
                double.TryParse(dataSupply.SelectedRows[0].Cells["ProdPriceSupply"].Value.ToString(), out PricePast);
                if (double.Parse(txtProdPrice.Text) != PricePast)
                {
                    int PastQuantity = Convert.ToInt32(dataSupply.SelectedRows[0].Cells["ProdQuantitySupply"].Value);

                    DateTime ProdPriceDate = DateTime.Now;
                    string ProdPriceDateString= ProdPriceDate.ToString("MM-yyyy");
                    con.Open();
                    cmd = new SqlCommand(@"insert into Prices 
                                        (ProdIDPrice,PriceDate,PastPrice,PriceQuantity,PriceDateMonth)
                                                Values
                                        ('" + txtProdID.Text + "','" + ProdPriceDate + "','" + PricePast + "','"+PastQuantity+"','"+ProdPriceDateString+"');", con);

                    cmd.ExecuteNonQuery();
                    con.Close();


                    con.Open();
                    cmd = new SqlCommand(@"Insert into Accounting 
                                    (AcctTotalPrice,AcctDateMonth,AcctQuantity)
                                     VALUES
                                     ('" + txtProdPrice.Text + "','" + ProdPriceDateString + "','1')", con);
                    cmd.ExecuteNonQuery();
                    con.Close();


                    con.Open();
                    cmd = new SqlCommand(@"Update Supply Set ProdConstantQuantity = 1, ProdQuantity = ProdQuantity + '"+PastQuantity
                                          +"' where ProdID = '"+txtProdID.Text+"';", con);
                    cmd.ExecuteNonQuery();
                    con.Close(); 
                }

1 个答案:

答案 0 :(得分:0)

制作价值' 1'对于AcctQuantity一个整数1,而不是它在引号内。