小编典典

Access SQL查询缺少更多必需参数

sql

在一个Web项目中,我试图执行以下查询:

SELECT ItemName as Name,
       ItemPicture as Picture,
       ItemHeroModif as Assistance,
       ItemTroopModif as Charisma,
       HerbCost as Herbs,
       GemCost as Gems
FROM Item WHERE ItemId = @value0

使用断点,可以看到我附加@value0了值2

尽管如此,我得到以下错误:

没有为一个或多个必需参数提供值。

我知道此错误通常是由于错误的SQL语法而产生的。我做的事有什么问题吗?

编辑

附件代码:

var madeForCommand = "SELECT ItemName as Name,ItemPicture as [Picture],ItemHeroModif as Assistance,ItemTroopModif as Charisma, HerbCost as Herbs, GemCost as Gems FROM Item WHERE ";
OleDbCommand command = new OleDbCommand();
for (int ii = 0; ii < items.Count; ii++)// items is a list of items with IDs I want to get from the query.
{
    madeForCommand += "ItemId =@value"+ii+" OR ";   
}
madeForCommand = madeForCommand.Substring(0, madeForCommand.Length - 4); // making sure I trim the final or; In the case I shown, it's just one item, so there are none at all.

然后:

OleDbCommand forOperations = new OleDbCommand(madeForCommand, _dbConnection); //_dbConnection is the connection to the database, it seems to work pretty well.
for (int ii = 0; ii < items.Count; ii++) 
{
    string attach = "@value" + ii;
    command.Parameters.AddWithValue(attach, items[ii].ID);
}

我很确定items[ii].ID这很好,断点表明它等于2,附件运行良好。

编辑2 :我已经按照Krish和Hans的建议 编辑 了代码,并且得到以下查询,没有任何附件:

SELECT ItemName as [Name],ItemPicture as Picture,ItemHeroModif as Assistance,ItemTroopModif as Charisma, HerbCost as Herbs, GemCost as Gems FROM [Item] WHERE (ItemID in (2));

如果更改任何内容,我仍然会收到相同的错误。

编辑3 :在Access中执行查询要求我给参数“ ItemPicture”赋一个值。ItemPicture是一列,不是吗?


阅读 242

收藏
2021-05-23

共1个答案

小编典典

NameItemPicture是Access查询中的问题词。将它们括在方括号中:

SELECT ItemName as [Name], ItemPicture as [Picture], ItemHeroModif as Assistance, ItemTroopModif as Charisma, HerbCost as Herbs, GemCost as Gems
FROM [Item] WHERE ItemID in (2);

由于用括号括起来的名称仍然使您缺少参数投诉,因此我要求您在Access的查询设计器中测试该查询。在这种情况下,Access提供了一个参数输入框,其中还包含Access解释为参数的单词。

您报告Access认为 ItemPicture 是一个参数。因此,通过在Access Design View中检查该表,您发现实际的字段名称为
ItemImageURL

SELECT ItemName as [Name], ItemImageURL as [Picture], ItemHeroModif as Assistance, ItemTroopModif as Charisma, HerbCost as Herbs, GemCost as Gems
FROM [Item] WHERE ItemID in (2);
2021-05-23