2017-04-16 26 views
3

我有这个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 must be defined so do @tmp

然而,当我在MySQL上运行此(HeidiSQL)它没有问题。

当我加入到New ConnectionStringAllow User Variables = true错误是:

Keyword not supported. Parameter name: allowuservariables

这是我ConnectionString我放在一起使用Connection Strings

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

我使用的MySQL版本5.6.21

+0

我想说的是摆脱查询中的Set @ qty部分,然后向名为'@ qty'的'cmdTemp.Parameters'集合中添加一个参数。 –

+0

请帮我解决这个!,即使我已经在使用'cmdTemp.Parameters'它仍然给我和错误@ChrisDunaway – Shiroze

+0

你不知道你在做什么。 “IF”功能中的作业的目的是什么? –

回答

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

I found this blog , which tells, that with newer versions of .net Connector you have to add

;Allow User Variables=True 

This answer

It's a connection string option - "Allow User Variables=true"

When set to true, parameters are prefixed with '?'.

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

0

我不清楚你想要做什么。看起来你可以简化你的查询,因为你的if函数总是返回相同的值,无论它们评估为真还是假。我对MySql不是很熟悉,所以我可能误解了你正在努力完成的任务。

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

然后只通过@qty作为参数。

+0

没有错,它更新所有的值,不检查然后更新。 @ChrisDunaway – Shiroze