【发布时间】:2015-04-26 05:51:35
【问题描述】:
我正在使用 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: - shopitem 类:
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.
}
【问题讨论】:
-
请添加一些代码
-
似乎是一个很容易解决的问题,但我们肯定需要看代码:-)
-
@SaagarEliasJacky 完成。
-
@PaoloCosta 很高兴听到 - 已编辑 (:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。