“,”附近的语法不正确。错误

发布于 2024-12-10 21:13:50 字数 438 浏览 0 评论 0原文

当我尝试执行此操作时,出现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白衬杉格子梦 2024-12-17 21:13:51

它可能使用逗号来格式化 totalprice,例如 3,14,而 SQL Server 需要一个点,例如 3.14

解决此问题的一种方法是指定 InvariantCulture,它使用点:

var s = string.Format(
    CultureInfo.InvariantCulture, 
    "SELECT * from orders where OrderUserId = {0} and OrderTotalPrice = {0:0.0}",
    42, 3.1415);

这会将任何计算机上的价格格式设置为 3.1

顺便说一句,将变量作为参数传递要好得多:

var com = new SqlCommand();
com.CommandType = CommandType.Text;
com.CommandText = "SELECT * from orders where OrderUserId = @userid " +
    "and OrderTotalPrice = @totalprice";
com.Parameters.AddWithValue("@userid", 42);
com.Parameters.AddWithValue("@totalprice", 3.1415);
var temporderid = com.ExecuteScalar();

这样您就不必担心格式,因为您向数据库发送的是双精度数,而不是格式化为字符串的双精度数。

It's probably formatting totalprice with a comma, like 3,14, where SQL Server expects a dot, like 3.14.

One way to fix that would be to specify InvariantCulture, which uses a dot:

var s = string.Format(
    CultureInfo.InvariantCulture, 
    "SELECT * from orders where OrderUserId = {0} and OrderTotalPrice = {0:0.0}",
    42, 3.1415);

This formats the price as 3.1 on any machine.

By the way, it's much nicer to pass the variables as parameters:

var com = new SqlCommand();
com.CommandType = CommandType.Text;
com.CommandText = "SELECT * from orders where OrderUserId = @userid " +
    "and OrderTotalPrice = @totalprice";
com.Parameters.AddWithValue("@userid", 42);
com.Parameters.AddWithValue("@totalprice", 3.1415);
var temporderid = com.ExecuteScalar();

Then you don't have to worry about formats because you send the database a double, not a double formatted as a string.

嘿哥们儿 2024-12-17 21:13:51

我猜这是因为当您连接查询时,totalprice 会转换为类似 12,35 的内容。

因此,我建议您使用参数化查询。例如这样:

var s = "SELECT * from orders " +
    " where OrderUserId = @userid and OrderTotalPrice = @totalprice";
var cmd = new SqlCommand(s, con);
cmd.Parameters.AddWithValue("userid", userid);
cmd.Parameters.AddWithValue("totalprice", totalprice);

int temporderid = Convert.ToInt32(cmd.ExecuteScalar());

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:

var s = "SELECT * from orders " +
    " where OrderUserId = @userid and OrderTotalPrice = @totalprice";
var cmd = new SqlCommand(s, con);
cmd.Parameters.AddWithValue("userid", userid);
cmd.Parameters.AddWithValue("totalprice", totalprice);

int temporderid = Convert.ToInt32(cmd.ExecuteScalar());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文