【发布时间】:2020-01-26 22:10:00
【问题描述】:
尝试在我的 c# 代码中构建动态 SQL 字符串。不知道我哪里出错了。
private void Save_Item_Drop_Data()
{
string uri = string.Empty;
string strUpdate = string.Empty;
WBGUpdateValue Update = new WBGUpdateValue();
Update.Email = Game_Data._User_Account.Email;
try
{
foreach (Base_Item item in Game_Data._Item_Drop)
{
//Recipe
if (item.Name.Contains("Recipe"))
{
if (Update_Recipe(item.Name))
{
uri = Game_Data._EndPoint + "api/WhiteboxGaming/Post_UnlockRecipe";
strUpdate = strUpdate + "UPDATE Skill_Gemology SET " + item.Name + "= 1 WHERE Email = ''" + Update.Email + "'' ";
_txtGameMessage.text = item.Name + " Unlocked";
}
}
//Rune
else if (item.Name.Contains("Rune") || item.Name == "Polished_Ancient_Stone" || item.Name == "Ancient_Stone")
{
uri = Game_Data._EndPoint + "api/WhiteboxGaming/Post_SaveItemdrop";
strUpdate = strUpdate + "UPDATE Inventory_Runes SET " + item.Name + "=" + item.Name + " + " + item.Count.ToString() + " WHERE Email = ''" + Update.Email + "'' ";
_txtGameMessage.text = "Found Rune";
}
Debug.Log(Update);
}
}
catch (Exception ex)
{
Debug.Log(ex);
}
if (Game_Data._Item_Drop.Count > 0)
{
Update.Value_1 = strUpdate;
StartCoroutine(booleanwebrequest(uri, Update));
}
Game_Data._Item_Drop = new List<Base_Item>();
}
字符串构建了这个:
UPDATE Inventory_Runes SET Polished_Ancient_Stone=Polished_Ancient_Stone + 1 WHERE Email = ''help.whiteboxgaming@gmail.com''
当我通过 SQL 运行它时,我得到一个错误:
消息 911,第 16 级,状态 4,第 74 行
数据库 'UPDATE Inventory_Runes SET Polished_Ancient_Stone=Polished_Ancient_Stone + 1 WHERE Email = 'help' 不存在。确保名称输入正确。
我了解它告诉我的内容,但我不确定如何修复我的 c# 代码中的电子邮件部分。
更新:现在我真的很困惑。我的动态 SQL 是正确的,但它仍然失败。
更新:
//Rune
else if (item.Name.Contains("Rune") || item.Name == "Polished_Ancient_Stone" || item.Name == "Ancient_Stone")
{
uri = Game_Data._EndPoint + "api/WhiteboxGaming/Post_SaveItemdrop";
strUpdate = strUpdate + "UPDATE Inventory_Runes SET [" + item.Name + "] = [" + item.Name + "] + " + item.Count.ToString() + " WHERE Email = ''' + @Email + '''";
_txtGameMessage.text = "Found Rune";
}
输出:
'UPDATE Inventory_Runes SET [Polished_Ancient_Stone] = [Polished_Ancient_Stone] + 4 WHERE Email = ''' + @Email + '''UPDATE Inventory_Runes SET [Rune_sa] = [Rune_sa] + 1 WHERE Email = ''' + @Email + '''UPDATE Inventory_Runes SET [Polished_Ancient_Stone] = [Polished_Ancient_Stone] + 1 WHERE Email = ''' + @Email + '''UPDATE Inventory_Runes SET [Ancient_Stone] = [Ancient_Stone] + 2 WHERE Email = ''' + @Email + '''UPDATE Inventory_Runes SET [Ancient_Stone] = [Ancient_Stone] + 1 WHERE Email = ''' + @Email + ''''
当我把它撕开时,它看起来像这样:
UPDATE Inventory_Runes SET [Polished_Ancient_Stone] = [Polished_Ancient_Stone] + 4 WHERE Email = 'help.whiteboxgaming@gmail.com'
UPDATE Inventory_Runes SET [Rune_sa] = [Rune_sa] + 1 WHERE Email = 'help.whiteboxgaming@gmail.com'
UPDATE Inventory_Runes SET [Polished_Ancient_Stone] = [Polished_Ancient_Stone] + 1 WHERE Email = 'help.whiteboxgaming@gmail.com'
UPDATE Inventory_Runes SET [Ancient_Stone] = [Ancient_Stone] + 2 WHERE Email = 'help.whiteboxgaming@gmail.com'
UPDATE Inventory_Runes SET [Ancient_Stone] = [Ancient_Stone] + 1 WHERE Email = 'help.whiteboxgaming@gmail.com'
【问题讨论】:
-
你能在变量初始化后放一个断点并检查
Update.Email的值吗? -
该代码中有几个错误。您可以通过using parameters 解决几乎所有问题,而不是尝试通过字符串连接来组装 SQL。
-
也许将
''" + Update.Email + "''更改为'" + Update.Email + "'? -
xkcd.com/327 - 使用参数而不是这样构造查询
标签: c# .net sql-server tsql