我正在嘗試連接表并從文本框中加載具有特定值的資料,但它給出了以下錯誤:“Boxing.exe 中發生了型別為‘System.NullReferenceException’的例外,但未在用戶代碼中處理
附加資訊:未將物件參考設定為物件的實體。”
我的代碼:
Dim Joinloin As New MySqlCommand("SELECT boxno, specie, netweight, producttype, loin FROM loins, boxing WHERE loins.code = boxing.loin AND type = '" & Label9.text & "' ORDER BY loincode", conn.getConnection)
我試圖在沒有 "type = '" & Label9.text & "'" 的情況下運行,并且運行良好。
uj5u.com熱心網友回復:
因為“Type”是 SQL 中的保留字,您需要在 SQL 資料庫和查詢中將其更改為“TypeX”之類的內容,然后重試。
uj5u.com熱心網友回復:
連接和命令需要呼叫它們的 Dispose 方法,以便它們可以釋放非托管資源。為此,應在使用它們的方法中宣告它們。Using...End Using 塊處理宣告和關閉/處理。
不要連接字串來構建 sq 陳述句。始終使用引數。
您的連接語法在上一千年中消失了。我對哪個欄位屬于哪個表進行了瘋狂的猜測。真的有一個叫loincode的欄位嗎?
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim dt = GetProductData(Label9.Text)
'Do something with the data table
End Sub
Private Function GetProductData(type As String) As DataTable
Dim dt As New DataTable
Using cn As New MySqlConnection("You connection string"),
cmd As New MySqlCommand("SELECT boxing.boxno, loins.specie, boxing.netweight, loins.producttype, boxing.loin
FROM boxing
JOIN loins ON boxing.loin = loins.code Where loins.[type] = @Type
ORDER BY boxing.loincode", cn)
cmd.Parameters.AddWithValue("@Type", type)
Using reader = cmd.ExecuteReader
dt.Load(reader)
End Using
End Using
Return dt
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/335619.html
