如何在vb.net10中执行此mysql查询,其中包含:=运算符

0yycz8jy  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(521)

我有一个sql语句:

CREATE TABLE D1 AS (
    SELECT 
        q1.ID,q1.ItemName, q1.Purchase, q1.Sale,
        CASE 
            WHEN @itemname:=q1.ItemName THEN 
                @runtot := @runtot + (q1.Purchase + q1.Sale) 
            ELSE @runtot:=(q1.Purchase + q1.Sale)End AS ClosingBalance,          
        @itemname := q1.ItemName as Item 
    FROM 
        (SELECT 
             ID AS ID, ItemName AS ItemName, 
             Purchase AS Purchase, Sale AS Sale
         FROM 
             itemledger 
         ORDER BY
             ItemName) AS q1)

在命令行中,这个查询非常有效。当我把它放在vb.net代码中时,它就不工作了。

Dim query922 = "CREATE TABLE D1 AS (SELECT q1.ID,q1.ItemName,q1.Purchase,q1.Sale, CASE WHEN @itemname=q1.ItemName THEN @runtot := @runtot + (q1.Purchase + q1.Sale) ELSE @runtot:=(q1.Purchase + q1.Sale)End AS ClosingBalance, @itemname:=q1.ItemName as Item FROM (SELECT ID AS ID,ItemName AS ItemName,Purchase AS Purchase,Sale AS Sale  FROM  itemledger ORDER  BY ItemName) AS q1) "
On Error Resume Next
Using connection922 As New MySqlConnection(connectionString922)
    Dim command922 As New MySqlCommand(query922, connection922)

    command922.Connection.Open()
    command922.ExecuteScalar()
    command922.Connection.Close()
End Using

sql语法有错误;请查看与您的mysql服务器版本对应的手册,以获得正确的语法来使用':=0+(q1.purchase+q1.sale)或者0:=(q1.purchase+q1.sale)在第1行以closingbala'结尾
我也试过这个:

Dim query922 = "CREATE TABLE D1 AS (SELECT q1.ID,q1.ItemName,q1.Purchase,q1.Sale, CASE WHEN @itemname=q1.ItemName THEN @runtot := @runtot + (q1.Purchase + q1.Sale) ELSE @runtot:=(q1.Purchase + q1.Sale)End AS ClosingBalance, @itemname:=q1.ItemName as Item FROM (SELECT ID AS ID,ItemName AS ItemName,Purchase AS Purchase,Sale AS Sale  FROM  itemledger ORDER  BY ItemName) AS q1) "
On Error Resume Next
Using connection922 As New MySqlConnection(connectionString922)
    Dim command922 As New MySqlCommand(query922, connection922)

    command922.Parameters.AddWithValue("@runtot", 0)
    command922.Parameters.AddWithValue("@itemname", "NULL")
    command922.Connection.Open()
    command922.ExecuteScalar()
    command922.Connection.Close()
End Using
bvjxkvbb

bvjxkvbb1#

this“:”运算符在支持.net 4版本的visual studio 10中不起作用,因为代码需要更高版本的.net framework。。。。

相关问题