C#oledbconnection更新不起作用

时间:2014-08-20 11:41:05

标签: c# oledb

有问题的代码:

public static void updateItemAmount(int item_id, int delta)
{
    using (OleDbCommand cmd = connection.CreateCommand())
    {
        // create command with placeholders
        cmd.CommandText =
            "UPDATE Items SET amount_total = amount_total+@delta WHERE item_number=@item_id;";

        // add named parameters
        cmd.Parameters.AddRange(new OleDbParameter[]
        {
            new OleDbParameter("@item_id",item_id),
            //new OleDbParameter("@delta",delta),

        });
        cmd.Parameters.Add("@delta", OleDbType.Integer).Value = delta;

        // execute
        if (cmd.ExecuteNonQuery() == 0)
            MessageBox.Show("Error");
    }
}

我一直得到"错误"即使我验证item_id是正确的消息。那是为什么?

1 个答案:

答案 0 :(得分:0)

几分钟后我发现了问题。显然,参数必须按顺序绑定(?)。这修好了它:

            cmd.Parameters.AddRange(new OleDbParameter[]
            {
                new OleDbParameter("@delta",delta),
                new OleDbParameter("@item_id",item_id),

            });
            //cmd.Parameters.Add("@delta", OleDbType.Integer).Value = delta;