简体   繁体   English

ASP.NET MVC C#Razor SQL 1 of 2几乎相同的更新查询不起作用

[英]ASP.NET MVC C# Razor SQL 1 of 2 almost identical Update Queries doesn't work

I am making an application to keep our stockroom easier to use. 我正在申请以使我们的储藏室更易于使用。
I made two methods to change some values in my database. 我使用两种方法来更改数据库中的某些值。 one to change te stock of an item and one to change the location where this item is stored. 一个可以更改物料的库存,另一个可以更改物料的存储位置。
for some reason when I use the first method the program changes the stock, but when I use the second method it won't change anything... 由于某种原因,当我使用第一种方法时,程序会更改库存,但是当我使用第二种方法时,它将不会更改任何内容...
I really don't get it cuz they're almost identical. 我真的不明白,因为它们几乎是相同的。
Is there anyone who sees something I do not? 有没有人看到我看不到的东西?

public void ChangeStock(double value, string Number)
    {
        string ConnString = "[connectionstring]";
        string SqlString = "Update Item Set Stock = ? WHERE ItemNumber= ?";
        using (OleDbConnection conn = new OleDbConnection(ConnString))
        {
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("Stock", value);
                cmd.Parameters.AddWithValue("ItemNumber", Number);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
    }

    public void ChangeLocation(string Location, string Number)
    {
        string ConnString = "[connectionstring]";
        string SqlString = "Update Item Set Location = ? WHERE ItemNumber = ?";
        using (OleDbConnection conn = new OleDbConnection(ConnString))
        {
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("ItemNumber", Number);
                cmd.Parameters.AddWithValue("Location", Location);       
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
    }

Since you're using OleDb, Access ignores the parameter names . 由于您使用的是OleDb,因此Access会忽略参数名称 Therefore you must supply the parameter values in the order Access expects them, which is the order in which the parameter placeholders appear in the CommandText. 因此,您必须按Access期望的顺序提供参数 ,这是参数占位符在CommandText中出现的顺序。

In the first UPDATE case, you're supplying the parameter values in the correct order. 在第一种UPDATE情况下,您将以正确的顺序提供参数值。 However, in the second case, you're supplying them in the opposite order compared to what Access expects. 但是,在第二种情况下,您将以与Access期望相反的顺序提供它们。 Use this order ... 使用此命令...

cmd.Parameters.AddWithValue("Location", Location);
cmd.Parameters.AddWithValue("ItemNumber", Number);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM