【发布时间】:2014-02-02 19:06:08
【问题描述】:
我想知道这段代码有什么问题。 我得到了问题:“,”附近的错误语法错误
它接受一个项目代码和一个项目对象。
public override int Update(string ItemCode, Item Item)
{
int rc = 0;
try
{
_sqlConnection = new SQLiteConnection(_connStr);
_sqlConnection.Open();
我认为问题在于下一条语句:
string updateQuery = string.Format("UPDATE ItemDatabase SET _itemDescription=@ItemDescription, _ItemPrice=@ItemPrice, _ItemName=@ItemName WHERE [_itemDescription] = '{0}' AND [_itemPrice] = '{1}' AND [_itemName] = '{2}'", Item.ItemDescription, Item.ItemPrice, Item.ItemName);
我不确定是否应该用逗号分隔。
SQLiteCommand sqlCommand = new SQLiteCommand(updateQuery, _sqlConnection);
SQLiteParameter[] sqlParams = new SQLiteParameter[]
{
new SQLiteParameter("@ItemDescription",DbType.String),
new SQLiteParameter("@ItemPrice",DbType.String),
new SQLiteParameter("@ItemName", DbType.String)
}; // end SQLiteParameter
sqlParams[0].Value = Item.ItemDescription;
sqlParams[1].Value = Item.ItemPrice;
sqlParams[2].Value = Item.ItemName;
sqlCommand.Parameters.AddRange(sqlParams);
rc = sqlCommand.ExecuteNonQuery();
if (rc == 1)
{
rc = 0;
}
_sqlConnection.Close();
}
catch (SQLiteException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
return rc;
}
【问题讨论】:
-
调试代码时看到的
updateQuery? -
为什么要在字段前面加上下划线?
-
这里有些地方不太对劲。用于参数的值与用于查询的值相同。所以你没有找到任何东西,或者你将字段更新为相同的值
-
在表格中我的字段名称用下划线命名。我在他们的代码中加入了“and”并删除了逗号,然后它给了我这个消息:Insufficient parameters provided to the command
-
我现在更改了代码,它说项目更新但数据没有改变
标签: c# sqlite insert sql-update