0

我一直在寻找和调试这段代码,但看不到任何错误。目前的错误是No Value given for one or more required Parameters

唯一需要的列是prodName并且始终检查以确保字符串不为空,否则它可能是我的整数和货币值的长度参数吗?我只是想让它们尽可能大以接受最合理大小的数据字段。

我还担心我的布尔值不适合“为空,不为空”类别,但如果未单击复选框,则会返回 false。

这是我的参数,如果你们简单地查看一下我可能犯的语法或逻辑错误,我将不胜感激,我在 c# 中相当中级

public int updateProducts(int prodId, string productName, string supplier, string category, string quantityPerUnit, string unitPrice, string unitsInStock, string unitsOnOrder, string reorderLevel, bool discontinued)
{
    int rows = 0;
    int PId = prodId;
    string prodName = productName;
    int supp = int.Parse(supplier);
    int? categoryID = 0;
    string qPerUnit = quantityPerUnit;
    Decimal? uPrice = 0;
    int? uInStock = 0;
    int? uOnOrder = 0;
    int? reorderLvl = 0;
    bool disc = discontinued;
    if (!string.IsNullOrWhiteSpace(unitPrice))
    {
        uPrice = Decimal.Parse(unitPrice);
    }
    if (!string.IsNullOrWhiteSpace(category))
    {
        categoryID = int.Parse(category);
    }
    if (!string.IsNullOrWhiteSpace(unitsInStock))
    {
        uInStock = int.Parse(unitsInStock);
    }
    if (!string.IsNullOrWhiteSpace(unitsOnOrder))
    {
        uOnOrder = int.Parse(unitsOnOrder);
    }
    if (!string.IsNullOrWhiteSpace(reorderLevel))
    {
        reorderLvl = int.Parse(reorderLevel);
    }

    string dbString = "UPDATE [Products] SET [ProductName]= ?,[Supplier]= ?,[Category]= ?,[QuantityPerUnit]= ?,[UnitPrice]= ?,[UnitsInStock]= ?,[UnitsOnOrder]= ?,[ReorderLevel]= ?, [Discontinued]= ? WHERE [SupplierID]= " + PId;

    try
    {
        conn.Open();
        oleCommand = new OleDbCommand(dbString, conn);

        oleCommand.Parameters.Add(new OleDbParameter("@prodName", OleDbType.VarChar, 30, ParameterDirection.Input, false, 10, 0, "ProductName", DataRowVersion.Original, null)).Value = prodName;
        oleCommand.Parameters.Add(new OleDbParameter("@supp", OleDbType.Integer, 40, ParameterDirection.Input, false, 10, 0, "Supplier", DataRowVersion.Original, null)).Value = supp;
        oleCommand.Parameters.Add(new OleDbParameter("@cat", OleDbType.Integer, 30, ParameterDirection.Input, true, 10, 0, "Category", DataRowVersion.Original, null)).Value = categoryID.HasValue ? (object)categoryID : DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@qPU", OleDbType.VarChar, 20, ParameterDirection.Input, true, 10, 0, "QuantityPerUnit", DataRowVersion.Original, null)).Value = string.IsNullOrWhiteSpace(qPerUnit) ? DBNull.Value : (object)qPerUnit;
        oleCommand.Parameters.Add(new OleDbParameter("@uPrice", OleDbType.Currency, 6, ParameterDirection.Input, true, 10, 0, "UnitPrice", DataRowVersion.Original, null)).Value = uPrice.HasValue ? (object)uPrice : DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@uInStock", OleDbType.Integer, 50, ParameterDirection.Input, true, 10, 0, "UnitsInStock", DataRowVersion.Original, null)).Value = uInStock.HasValue ? (object)uPrice : DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@uOnOrder", OleDbType.Integer, 50, ParameterDirection.Input, true, 10, 0, "UnitsOnOrder", DataRowVersion.Original, null)).Value = uOnOrder.HasValue ? (object)uOnOrder : DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@reorderLvl", OleDbType.Integer, 50, ParameterDirection.Input, true, 10, 10, "ReorderLevel", DataRowVersion.Original, null)).Value = reorderLvl.HasValue ? (object)reorderLvl : DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@discontinued", OleDbType.Boolean, 10, ParameterDirection.Input, true, 10, 0, "Discontinued", DataRowVersion.Original, null)).Value = disc;
        rows = (int)oleCommand.ExecuteNonQuery();

    }
    catch (Exception ex)
    {
        dbError = "Add Supplier command Error: " + ex.Message;
    }
    finally
    {

        conn.Close();
    }
    return rows;
}
4

1 回答 1

0

是方括号。方括号表示 Jet Sql 的参数。但这也意味着“这个标识符里面有一个空格”

您的所有标识符都没有空格,因此只需删除方括号即可。如果您确实有带空格的标识符,则必须完全限定名称。

参考:JET SQL for Access 2003

于 2012-11-29T16:14:33.087 回答