Mysql 未定义参数值时,如何定义参数值?

Mysql 未定义参数值时,如何定义参数值?,mysql,vb.net,Mysql,Vb.net,我对这个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) " & _

我对这个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)上运行这个时,它没有问题

当我添加到
新连接字符串中时,
允许用户变量=true
错误为:

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

这是我的
连接字符串
,我使用以下方法将其组合在一起:


我正在使用MySQL 5.6.21版

我不清楚您想做什么。看起来您可以简化查询,因为无论函数的计算结果是true还是false,
if
函数总是返回相同的值。我对MySql不是很熟悉,所以我可能误解了您想要实现的目标

update(tb_harvest) 
set actual = if(@qty >= actual, 0, actual - @qty), 
    Status = if(@qty >= actual, 0, 1)
order by harvestid;
然后只需将
@qty
作为参数传入即可。

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

Server=myserver地址;数据库=myDataBase;Uid=我的用户名;Pwd=我的密码;AllowUserVariables=True;
注意
AllowUserVariables
。这是在连接字符串中设置它的方式。然而,这似乎给你带来了悲伤,尽管我不明白为什么,因为这也是在报告中所说的。可能是版本特定的

但是,请尝试将其更改为:

;允许用户变量=True
这是它在连接字符串中的外观:

Server=localhost;端口=3306;数据库=测试;Uid='test';普华永道‌​测试';允许用户变量‌​bles=真;
我四处查看了一下,发现了一些其他来源,也将其设置为
;允许用户变量=True

:

$connectionstring=“服务器=$Server;端口=$Port;数据库=$Database;Uid=$User;Pwd=$Password;允许零日期时间=yes;允许用户变量=True”
:

我发现了这一点,这说明,对于较新版本的.net连接器,您必须添加

;允许用户变量=True
:

这是一个连接字符串选项-“允许用户变量=true”

设置为true时,参数的前缀为“?”


OP已确认他们不必使用
。他们继续在查询中使用
@

我想说的是,去掉查询中的
设置@qty
部分,然后向名为
@qty
cmdTemp.Parameters
集合添加一个参数。请帮我解决这个问题!,即使我已经在使用
cmdTemp.Parameters
它仍然会给我错误@chrisdunaway它不清楚你在尝试做什么。
IF
函数中赋值的目的是什么?我试图完成的是当按下按钮时,它运行上面的代码。在数据库上,qty值从第一行更新每一行,直到输入的数量达到0。例如:
Qty=500
第1行Qty=300
第2行Qty=400
如果代码运行
第1行Qty=0,第2行Qty=200
清除@chrisdunaway使用
@
,此@bug中没有错误。请更新所有值,不检查然后更新@克里斯杜奈
update(tb_harvest) 
set actual = if(@qty >= actual, 0, actual - @qty), 
    Status = if(@qty >= actual, 0, 1)
order by harvestid;