【问题标题】:How to define a parameter value when the value is not defined?未定义值时如何定义参数值?
【发布时间】:2017-09-12 07:59:35
【问题描述】:

我对这个 MySQL 命令有疑问:

cmdTemp = New MySqlCommand("SET @qty = " & Qty & "; update(tb_harvest) set actual = (case when @qty >= actual " & _
                           "then if(@qty := @qty - actual, 0, 0) when (@tmp := actual - @qty) " & _
                           "then if(@qty := 0, @tmp, @tmp) " & _
                           "else actual end), Status = (case when @qty >= actual then if(@qty := @qty - actual, 0, 0) " & _
                           "when (@tmp := actual - @qty) then if(@qty := 0, 1, 1) else 1 end) order by harvestid;", cn)

当我尝试在 VB.NET (VS2008) 中运行时,出现以下错误:

@Qty 必须定义,@tmp 也是如此

但是,当我在 MySQL(HeidiSQL) 上运行它时,它没有问题。

当我添加到New ConnectionStringAllow User Variables = true 时,错误是:

不支持关键字。参数名称:allowuservariables

这是我的ConnectionString,我使用Connection Strings 组合在一起:

Server=localhost;Port=3306;Database=testing;Uid='test';Pwd='‌​test';AllowUserVaria‌​bles=True;

我正在使用 MySQL 5.6.21 版

【问题讨论】:

  • 我会说去掉查询的Set @qty 部分,然后将参数添加到名为@qtycmdTemp.Parameters 集合中。
  • 请帮我解决这个问题!,即使我已经在使用cmdTemp.Parameters 它仍然给我和错误@ChrisDunaway
  • 不清楚您要做什么。 IF 函数内部赋值的目的是什么?
  • 我试图完成的是当按下按钮时,它运行上面的代码。在数据库中,数量值从第一行更新每一行,直到输入的数量达到 0。例如:Qty = 500row 1 qty=300row 2 qty=400 如果代码运行Row 1 qty = 0 and row 2 qty = 200 清除? @ChrisDunaway
  • 使用 @ ,这个@Bugs 没有错

标签: mysql vb.net


【解决方案1】:

我不清楚你想做什么。似乎您可以简化查询,因为您的 if 函数无论其计算结果为真还是假,总是返回相同的值。我对 MySql 不是很熟悉,所以我可能误解了您要完成的工作。

update(tb_harvest) 
set actual = if(@qty >= actual, 0, actual - @qty), 
    Status = if(@qty >= actual, 0, 1)
order by harvestid;

然后将@qty作为参数传入即可。

【讨论】:

  • 它更新所有值,而不是检查然后更新。 @ChrisDunaway
【解决方案2】:

Connection Strings 确实指定这是一个有效的连接:

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;AllowUserVariables=True;

通知AllowUserVariables。这就是您在连接字符串中设置它的方式。然而,这似乎让您感到悲伤,尽管我不明白为什么,因为这也是 documentation 中所述的内容。也许它是特定于版本的。

但是尝试将其更改为:

;Allow User Variables=True

这就是它在您的连接字符串中的样子:

Server=localhost;Port=3306;Database=testing;Uid='test';Pwd='‌​test';Allow User Varia‌​bles=True;

我环顾四周,发现其他一些来源也将其设置为;Allow User Variables=True

This answer:

$connectionstring =  "Server=$Server;Port=$port;Database=$DataBase;Uid=$User;Pwd=$Password;allow zero datetime=yes;Allow User Variables=True"

This answer:

我发现了这个blog,它告诉你,对于较新版本的 .net 连接器,您必须添加

;Allow User Variables=True

This answer:

这是一个连接字符串选项 - “Allow User Variables=true”

设置为 true 时,参数以'?'为前缀。

OP 有confirmed,他们不必使用?。他们继续在查询中使用@

【讨论】:

    猜你喜欢
    • 2019-10-05
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多