必填参数未赋值,有值
No value given for required parameters, with value
我正在使用 c#/ASP.NET 开发 Web 应用程序。
作为应用程序的一部分,我想连接到 ms-access 数据库,并将其中的值放入 dataset
.
出于某种原因,我在用 DataAdaptor
填充 dataset
时出现标题错误 - 尽管如此,当我使用断点时,我可以看到命令如下:
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: - 商店商品 class:
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.
}
这是您应该使用的代码:
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 中,参数是使用 ?并且必须按照查询中使用的相同顺序提供。
我正在使用 c#/ASP.NET 开发 Web 应用程序。
作为应用程序的一部分,我想连接到 ms-access 数据库,并将其中的值放入 dataset
.
出于某种原因,我在用 DataAdaptor
填充 dataset
时出现标题错误 - 尽管如此,当我使用断点时,我可以看到命令如下:
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: - 商店商品 class:
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.
}
这是您应该使用的代码:
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 中,参数是使用 ?并且必须按照查询中使用的相同顺序提供。