在网络项目中,我尝试执行以下查询:
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中执行查询要求我给参数&#34; ItemPicture&#34; ...奇数; ItemPicture是一个专栏,不是吗?
答案 0 :(得分:3)
Name
,Item
和Picture
为problem words。将它们括在方括号中:
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&#39;中测试该查询。查询设计师。在该上下文中,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);
答案 1 :(得分:2)
您没有从应用程序外部获取Where条件,因此字符串连接是安全的。 (至少我是这么认为的)
只需添加如下参数:
var madeForCommand = "SELECT ItemName as Name,ItemPicture as Picture,ItemHeroModif as Assistance,ItemTroopModif as Charisma, HerbCost as Herbs, GemCost as Gems " +
"FROM Item WHERE (ItemID in (";
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.
{
if (i<=1) {
madeForCommand += items[ii].ID
}else {
madeForCommand += "," + items[ii].ID;
}
}
madeForCommand += "))"
最后你将得到一个SQL查询:
"SELECT ItemName as Name,ItemPicture as Picture,ItemHeroModif as Assistance,ItemTroopModif as Charisma, HerbCost as Herbs, GemCost as Gems " +
"FROM Item WHERE (ItemID in (1,2,3))";
答案 2 :(得分:1)
MS Access不知道这样的参数:@value0
!
使用?
代替named parameters,如下所示:
PARAMETERS [value0] INT;
SELECT ItemName as Name,
ItemPicture as Picture,
ItemHeroModif as Assistance,
ItemTroopModif as Charisma,
HerbCost as Herbs,
GemCost as Gems
FROM Item WHERE ItemId = [value0];
如何调用此查询?请阅读:Accessing Microsoft Office Data from .NET Applications
有关详细信息,请参阅:OleDbCommand.Parameters Property
<强> [编辑] 强>
根据我的理解,您希望将多个ItemId
传递给查询。所以,你必须使用IN子句:
SELECT ...
FROM ...
WHERE ItemID IN (1, 2, 5, 8)