“,”附近的语法不正确。错误
当我尝试执行此操作时,出现 SQL 错误:
public static int GetOrderId(decimal totalprice, int userid)
{
string s = "SELECT * from orders where OrderUserId = " + userid + " and OrderTotalPrice = " + totalprice;
cmd = new SqlCommand(s, con);
int temporderid = Convert.ToInt32(cmd.ExecuteScalar());
return temporderid;
}
据我所知,因为它返回了 OrderTotalPrice,所以格式不兼容。但我不知道如何以兼容的格式获取它。
I am getting an SQL error when I try to do this:
public static int GetOrderId(decimal totalprice, int userid)
{
string s = "SELECT * from orders where OrderUserId = " + userid + " and OrderTotalPrice = " + totalprice;
cmd = new SqlCommand(s, con);
int temporderid = Convert.ToInt32(cmd.ExecuteScalar());
return temporderid;
}
As far I can see its because it gets OrderTotalPrice back, the format is incompatible. But I cant figure out how to get it in the compatible format.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可能使用逗号来格式化
totalprice
,例如3,14
,而 SQL Server 需要一个点,例如3.14
。解决此问题的一种方法是指定 InvariantCulture,它使用点:
这会将任何计算机上的价格格式设置为
3.1
。顺便说一句,将变量作为参数传递要好得多:
这样您就不必担心格式,因为您向数据库发送的是双精度数,而不是格式化为字符串的双精度数。
It's probably formatting
totalprice
with a comma, like3,14
, where SQL Server expects a dot, like3.14
.One way to fix that would be to specify InvariantCulture, which uses a dot:
This formats the price as
3.1
on any machine.By the way, it's much nicer to pass the variables as parameters:
Then you don't have to worry about formats because you send the database a double, not a double formatted as a string.
我猜这是因为当您连接查询时,totalprice 会转换为类似
12,35
的内容。因此,我建议您使用参数化查询。例如这样:
I guess this is because totalprice gets converted to something like
12,35
when you concatenate the query.Therefore, I'd suggest you use a parametrized query. E.g. like this: