簡體   English   中英

vb.net中的字符串變量可以在SQL Where語句中作為引用嗎?

[英]Could string variable in vb.net be a reference in SQL Where statement?

有人可以在下面的WHERE聲明中為我提供幫助嗎? 我想在WHERE語句中將bn作為參考。

這是我的代碼中的內容:

Public bn As String = ""  

Dim SQLStatement As String = "UPDATE patient SET number_of_bottles='" & lblBottle.Text & "'  WHERE bednumber=bn ORDER BY patient_ID DESC LIMIT 1"

在程序執行過程中,bn是一個標識符,在此我可以知道要訪問哪個床號。

任何幫助,將不勝感激,謝謝!

(經編輯以使用特定於MySQL的對象,而不是通用ODBC)

    Dim bn As String = ""       ' Set this to some value in your code
    Dim bottles As Integer = 0  ' Set this to some value in your code
    Dim SQLStatement As String = "UPDATE patient SET number_of_bottles = @bottles WHERE bednumber = @bednumber"

    Using cnn As New MySqlConnection("Connection string here")
        Dim cmd As New MySqlCommand(SQLStatement, cnn)
        cmd.Parameters.AddWithValue("bottles", bottles)
        cmd.Parameters.AddWithValue("bednumber", bn)
        cnn.Open()
        cmd.ExecuteNonQuery()
        cnn.Close()
    End Using

備用版本,手動創建MySqlParameter對象-請注意,您需要創建參數對象,設置它們的值,然后將它們添加到MySqlCommand對象的參數集合中

Using cnn As New MySqlConnection("Connection string here")
    Dim cmd As New MySqlCommand(SQLStatement, cnn)
    Dim pBottles As New MySqlParameter("bottles", bottles)
    Dim pBedNumber As New MySqlParameter("bednumber", bn)
    cmd.Parameters.Add(pBottles)
    cmd.Parameters.Add(pBedNumber)
    cnn.Open()
    cmd.ExecuteNonQuery()
    cnn.Close()
End Using

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM