没有给出所需参数的值,值为

时间:2015-04-26 05:51:36

标签: c# ms-access oledb

我在网络应用上使用c#/ ASP.NET。 作为应用程序的一部分,我想连接到ms-access数据库,并从中获取值dataset

出于某种原因,在使用dataset填充DataAdaptor时,我在标题中出现错误 - 尽管如此,当我使用断点时,我可以看到命令如下: SELECT ..... WHERE ItemID = @value0(如果您需要参数,请求他们,我将复制整件事。)

我还可以看到@ value0的值为2,带有断点,我确信它是查询中唯一的值。

我的问题是,这怎么可能发生?如果查询中唯一的值被填充,我缺少什么?

修改: 完整查询: SELECT ItemName as Name,ItemPicture as Picture,ItemHeroModif as Assistance,ItemTroopModif as Charisma, HerbCost as Herbs, GemCost as Gems FROM Item WHERE ItemID = @value0"

完整的构建代码(为每个用户生成查询需要不同数量的项目,这个项目只有一个项目,所以我已经用它来测试):

static public DataSet getUserShopItemDS(string username,List<shopItem> items)
{
string madeForCommand = "SELECT ItemName as Name,ItemPicture as Picture,ItemHeroModif as Assistance,ItemTroopModif as Charisma, HerbCost as Herbs, GemCost as Gems FROM Item WHERE ItemID = ";
        int count = 0;
        foreach (shopItem item in items)
        {
            madeForCommand += "@value"+count+" OR ";
            count++;
        }
        madeForCommand = madeForCommand.Substring(0, madeForCommand.Length - 3);
        OleDbCommand command = GenerateConnection(madeForCommand);
        for (int ii = 0; ii < items.Count; ii++)
        {
            command.Parameters.AddWithValue("@value" + ii, items[ii].ID);
        }
        var FreestyleAdaptor = new OleDbDataAdapter();
        FreestyleAdaptor.SelectCommand = command;
        DataSet Items = new DataSet();
        FreestyleAdaptor.Fill(Items);//The error is thrown here.
        return Items;
    }

编辑2: - 购物项目类:

public class shopItem
{
    //Irrelevant parameters,
    public int ID // Whenever a user logs in, I create a list of his items, and attach each one it's ID from the database. I send the list as a parameter to the function.
    {
        get;
        private set;
    }
    public shopItem(int ID)
    {
        this.ID = ID;
    }
    public shopItem() { }
    // more constructors.
}

1 个答案:

答案 0 :(得分:1)

以下是您应该使用的代码:

        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++)
        {
            madeForCommand += "ItemId = ?  OR ";
            command.Parameters.AddWithValue("value" + ii, items[ii].ID);
        }

        madeForCommand = madeForCommand.Substring(0, madeForCommand.Length - 3);

在MS Access参数中使用?并且必须按照查询中使用的顺序提供。