更新语句没有任何作用。

huangapple go评论71阅读模式
英文:

Update statement does nothing

问题

当我在ChbBeds_numericUpDown中输入一个数字并点击“更新”按钮时,它会显示“数据已更新”,但数据库中没有任何变化。

private void ChbUp_button_Click(object sender, EventArgs e)
{
    try 
    {
        string statement = "UPDATE ChamberXPavilions SET Beds count = @beds_count WHERE Pav_name = @pav_name AND Chamber_number = @chamber_number";

        cmd = new OleDbCommand(statement, conn);

        cmd.Parameters.AddWithValue("@pav_name", Chbpav_comboBox.Text);
        cmd.Parameters.AddWithValue("@chamber_number", Chb_numericUpDown.Value);
        cmd.Parameters.AddWithValue("@beds_count", ChbBeds_numericUpDown.Value);

        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();

        MessageBox.Show("数据已更新");
        showdata();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

SQL语句有问题吗?

英文:

When I enter a number in the ChbBeds_numericUpDown and click on the "Update" button, it says "Data Updated", but nothing changes in the database

private void ChbUp_button_Click(object sender, EventArgs e)
{
    try 
    {
        string statement = "UPDATE ChamberXPavilions SET Beds count = @beds_count WHERE Pav_name = @pav_name AND Chamber_number = @chamber_number";

        cmd = new OleDbCommand(statement, conn);

        cmd.Parameters.AddWithValue("@pav_name", Chbpav_comboBox.Text);
        cmd.Parameters.AddWithValue("@chamber_number", Chb_numericUpDown.Value);
        cmd.Parameters.AddWithValue("@beds_count", ChbBeds_numericUpDown.Value);

        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();

        MessageBox.Show("Data updated");
        showdata();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Is the SQL statement wrong ?

答案1

得分: 1

与 SQL Server 相反,MS Access 的 OleDB 提供程序不支持命名参数,而是使用位置参数

在你的情况下,你有一个 SQL 语句:

UPDATE ChamberXPavilions 
SET Beds count = @beds_count 
WHERE Pav_name = @pav_name AND Chamber_number = @chamber_number

所以你需要按照相同的顺序提供参数 - 首先是 @beds_count,然后是 @pav_name,最后是 @chamber_number

因此,尝试使用以下代码提供参数值:

cmd = new OleDbCommand(statement, conn);

cmd.Parameters.AddWithValue("@beds_count", ChbBeds_numericUpDown.Value);    
cmd.Parameters.AddWithValue("@pav_name", Chbpav_comboBox.Text);
cmd.Parameters.AddWithValue("@chamber_number", Chb_numericUpDown.Value);

现在,你的 UPDATE 语句应该能够获取正确的值,并且应该可以正常工作。

英文:

Contrary to SQL Server, the OleDB provider for MS Access does NOT work with named parameters - instead, it uses positional parameters.

In your case, you have a SQL statement

UPDATE ChamberXPavilions 
SET Beds count = @beds_count 
WHERE Pav_name = @pav_name AND Chamber_number = @chamber_number

so you need to also provide the parameters in the same order - first @beds_count, then @pav_name and finally @chamber_number.

So try this for providing the parameter values:

cmd = new OleDbCommand(statement, conn);

cmd.Parameters.AddWithValue("@beds_count", ChbBeds_numericUpDown.Value);    
cmd.Parameters.AddWithValue("@pav_name", Chbpav_comboBox.Text);
cmd.Parameters.AddWithValue("@chamber_number", Chb_numericUpDown.Value);

Now, your UPDATE statement should get the proper values and should now work

huangapple
  • 本文由 发表于 2023年1月8日 21:54:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定