0

我有一个 Winform,它通过编辑两个 TextBox 产品名称和产品成本来更新 SQL 数据库,但是它不会更新数据库,这是我的示例代码

     private void simpleButton5_Click(object sender, EventArgs e)
    {
        string id = comboBox2.Items[comboBox2.SelectedIndex].ToString();
        string name = txtProdName.Text;
        string cost = txtProductCost.Text;
        cn.Open();

        string query = "UPDATE [Product1] SET [Product_Name]= @Product_Name,[Product_Cost]= @Product_Cost where [Product_ID]= @Product_ID";
        SqlCommand cmd = new SqlCommand(query, cn);
        cmd.Parameters.AddWithValue("@Product_ID", id);
        cmd.Parameters.AddWithValue("@Product_Name", name);
        cmd.Parameters.AddWithValue("@Product_Price", cost);
        try
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("Update Succesfully");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        cn.Close();

    }

id 的数据类型是char,Product_Name 是nvarchar(50),Porduct_Cost 是bigint。任何我会感激的想法

4

2 回答 2

1

看来你转换有错误,如果你的数据库中的成本字段是 bigint 然后转换

string cost = txtProductCost.Text

Int64 cost = Convert.Toint64(txtProductCost.Text);

Int64 直接映射到 BigInt。

或者如果它是 Double 然后转换

string cost = txtProductCost.Text

Double cost = Convert.ToDouble(txtProductCost.Text);

希望对你有效。

于 2013-04-25T11:55:16.633 回答
0

一、将字符串值转换为int

string cost = txtProductCost.Text;
costval=int.Parse(cost)// costval is integer

接下来,更新数据库

cmd.Parameters.AddWithValue("@Product_Price", costval);
于 2013-04-25T11:47:29.227 回答