Access SQL查询缺少更多必需参数 [英] Access SQL query missing more required parameters
问题描述
在一个Web项目中,我正在尝试执行以下查询:
In a web project, I'm trying to execute the following query:
SELECT ItemName as Name,
ItemPicture as Picture,
ItemHeroModif as Assistance,
ItemTroopModif as Charisma,
HerbCost as Herbs,
GemCost as Gems
FROM Item WHERE ItemId = @value0
有了断点,可以看到我将2
值附加到@value0
.
With breakpoints, I can see I attached to @value0
the value, 2
.
尽管如此,我得到以下错误:
Despite this, I get the following error:
没有为一个或多个必需参数提供值.
No value given for one or more required parameters.
我知道此错误通常是由于错误的SQL语法而产生的.我做的事有什么问题吗?
I understood this error is usually generated due to bad SQL syntax. Is there anything wrong with what I did?
编辑:
附件代码:
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,并且附件运行良好.
I'm pretty sure items[ii].ID
is fine, breakpoints show that it equals 2 and the attachment goes well.
编辑2 : 我已按照Krish和Hans的建议编辑了代码,并且得到以下查询,没有任何附件:
EDIT 2: I've editted the code as Krish and Hans advised me, and I get the following query without any attachments:
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));
如果发生任何更改,我仍然会遇到相同的错误.
I still get the same error, if it changes anything.
编辑3 : 在Access中执行查询,要求我为参数"ItemPicture"提供一个值. ItemPicture是一列,不是吗?
EDIT 3: Executing the query in Access asks me to give a value to the parameter "ItemPicture"... Odd; ItemPicture is a column, isn't it?
推荐答案
Name
, Item
, and Picture
are problem words in Access queries. Enclose them in square brackets:
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解释为参数的单词.
Since bracketing those names still gave you a missing parameter complaint, I asked you to test that query in Access' query designer. In that context, Access presents a parameter input box which also includes the word which Access interprets as a parameter.
您报告Access认为 ItemPicture 是一个参数.因此,通过在Access Design View中检查该表,您发现实际的字段名称为 ItemImageURL .
You reported Access thinks ItemPicture is a parameter. So by inspecting that table in Access Design View, you discovered the actual field name is 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);
这篇关于Access SQL查询缺少更多必需参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!