繁体   English   中英

如何在MySql查询中添加vb.net条件

[英]How to add vb.net condition inside MySql query

我正在使用Datagridview并获取单元格值。 我需要做的是,当一个pieces的细胞内的字符串值DatagridView话,我改变我的查询为“无”。 我已经在使用case查询,并且我认为这不是解决我的问题的正确答案。 你能帮我吗? 顺便说一下,这是我的代码,我已经完成了。

For k As Integer = 0 To dtgOrder.RowCount - 1
                objconn.Open()
                cmbQuery = "SELECT product_quantity.Quantity As 'quant_test' from product_quantity 
                            INNER join product_table on product_quantity.Product_ID = product_table.Product_ID 
                           where product_size.Size_Name
                           =  '" & If dtgOrder.Rows(k).Cells(3).Value = "pieces" Then 
                                   dtgOrder.Rows(k).Cells(3).Value = "None" 
                                   End If & "'"
                objcmd = New MySqlCommand(cmbQuery, objconn)
                objdr = objcmd.ExecuteReader
                While objdr.Read
                    quantitytesting = objdr.GetInt32("quant_test")
                End While
                objconn.Close()
Next

这是我已经在使用MySQL的Case进行的查询,我也已经在上述查询中应用了它。

    SELECT product_quantity.Quantity As 'quant_test' from product_quantity 
    INNER join product_table on product_quantity.Product_ID = product_table.Product_ID 
     where product_size.Size_Name
 =  (Case when '"& dtgOrder.Rows(k).Cells(3).Value = "pieces" &"' Then 'None' Else '"& dtgOrder.Rows(k).Cells(3).Value &"')"

首先,也是最重要的一点是要了解的是, 永远不要使用字符串连接将值放入SQL查询中。 到目前为止,您所拥有的代码只是乞求让您的数据库被黑客入侵。

顺便说一下,这里有两种可能的解决方案。

在VB中:

Const SQL As String = _
    "SELECT product_quantity.Quantity As 'quant_test' " & 
    " FROM product_quantity " &
    " INNER join product_table on product_quantity.Product_ID = product_table.Product_ID " &
    " WHERE product_size.Size_Name = @SizeName"
                       =  '
Using cn  As New MySlConnection("connection string here"), 
      cmd As New MySqlCommand(SQL, cn)

    Dim p As MySqlParameter = cmd.Parameters.Add("@SizeName", MySqlDbType.VarString)
    cn.Open()

    For Each row As DataGridViewRow In dtgOrder.Rows
        p.Value = If(row.Cells(3).Value = "pieces", "None", row.Cells(3).Value)
        Using objdr As MySqlDataReader = cmd.ExecuteReader()
            While objdr.Read
                quantitytesting = objdr.GetInt32("quant_test")
            End While
        End Using
    Next
End Using

与原始代码相比,此代码有许多重要的改进,因此请研究该模式以备将来使用。 但是,当我在这里时,我还要补充一点,在带有DataGridView的循环中看到这样的查询非常奇怪。 该DataGridView中的行几乎可以肯定来自数据库,这意味着您应该能够构建单个查询来产生结果,而根本不需要任何循环。 将其放入单个查询将比使数据库多次运行好得多

在SQL中:

Const SQL As String = _
    "SELECT product_quantity.Quantity As 'quant_test' " & 
    " FROM product_quantity " &
    " INNER join product_table on product_quantity.Product_ID = product_table.Product_ID " &
    " WHERE product_size.Size_Name = COALESCE(NULLIF(@SizeName, 'pieces'),'None')"
                       =  '
Using cn  As New MySlConnection("connection string here"), 
      cmd As New MySqlCommand(SQL, cn)

    Dim p As MySqlParameter = cmd.Parameters.Add("@SizeName", MySqlDbType.VarString)
    cn.Open()

    For Each row As DataGridViewRow In dtgOrder.Rows
        p.Value = row.Cells(3).Value
        Using objdr As MySqlDataReader = cmd.ExecuteReader()
            While objdr.Read
                quantitytesting = objdr.GetInt32("quant_test")
            End While
        End Using
    Next
End Using

在这两个I选项中,我更喜欢第一个...使客户端应用程序执行此特定工作,而不是数据库。 我发布此消息是因为,如果您确实尝试将其简化为单个SQL语句,则可能需要此逻辑才能使其正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM