c# – Acess SQL查询缺少更多必需参数

前端之家收集整理的这篇文章主要介绍了c# – Acess 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.

尽管如此,我收到以下错误

No value given for one or more required parameters.

我知道这个错误通常是由于错误sql语法而产生的.我做了什么有什么不对吗?

编辑:

附件代码

var madeForCommand = "SELECT ItemName as Name,ItemPicture as [Picture],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);
        }

我很确定项目[ii] .ID很好,断点显示它等于2并且附件顺利.

编辑2:
我编写了Krish和Hans建议的代码,我得到以下查询,没有任何附件:SELECT ItemName为[Name],ItemPicture为Picture,ItemHeroModif为Assistance,ItemTroopModif为Charisma,HerbCost为Herbs,GemCost为Gems FROM [项目] WHERE((2)中的ItemID);如果它改变了什么,我仍然会得到同样的错误.

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

解决方法

Access查询中的名称,项目和图片problem words.将它们括在方括号中:
SELECT ItemName as [Name],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],GemCost as Gems
FROM [Item] WHERE ItemID in (2);
原文链接:https://www.f2er.com/csharp/244832.html

猜你在找的C#相关文章