更新访问数据库行c#

时间:2017-02-25 17:52:32

标签: c# ms-access

我正在尝试制作一张表格来更新产品价格

表单在用户选择组合框时首先从数据库中获取数据并将其放在文本框中

但由于某种原因,按钮点击的更新查询无法正常工作,产品价格不会更新,也不会出现任何错误

这是代码

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        productsearch();
    }
    public void productsearch()
    {
        string query = @"select ID, Productprice  From Product where Productname LIKE @name";
        using (var conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
        using (var cmd = new System.Data.OleDb.OleDbCommand(query, conn))
        {
            cmd.Parameters.Add("@name", System.Data.OleDb.OleDbType.VarChar).Value = comboBox1.Text;
            conn.Open();

            using (var rdr = cmd.ExecuteReader())
            {
                if (rdr.Read())
                {
                    textBox2.Text = rdr["ID"].ToString();
                    textBox1.Text = rdr["Productprice"].ToString();
                }
                rdr.Close();
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string query = @"update Product set Productprice = @price where ID = @id";
        using (var conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
        using (var cmd = new System.Data.OleDb.OleDbCommand(query, conn))
        {
            cmd.Parameters.Add("@id", System.Data.OleDb.OleDbType.VarChar).Value = textBox2.Text;
            cmd.Parameters.Add("@price", System.Data.OleDb.OleDbType.Currency).Value = textBox3.Text;
            conn.Open();
            cmd.ExecuteNonQuery();

        }
    }
}

0 个答案:

没有答案