[英]How to get a specific data from mysql database vb.net
我的代碼在數據庫中搜索列表視圖中所選項目的相同值,例如:“ 01”,代碼在數據庫中搜索“ 01”,現在數據庫中的01等同於名稱,例如01 = Name,我的問題是 如果代碼找到01,我想獲取名稱而不是01。
我的密碼
Try
Call DatabaseConnection()
MySqlConn.Open()
For Each item As ListViewItem In ListViewAttendance.SelectedItems
Query = "select * from dtr_database.dtr_entries where dtr_entry_number= '" & item.SubItems(0).Text & "'"
Command = New MySqlCommand(Query, MySqlConn)
Reader = Command.ExecuteReader
Dim Count As Integer
Count = 0
While Reader.Read
Count = Count + 1
End While
If Count = 1 Then
'if 01 is found Get Name of 01. How to do this?
MessageBox.Show("Record Found")
ElseIf Count > 1 Then
MessageBox.Show("Multiple Records Found")
Else
MessageBox.Show("Record Found2")
End If
Next
MySqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
盡管您的代碼有很多問題,但直接的問題答案是您應該在While
循環內調用Reader.GetString(ColumnNumber)
以獲取Name
列的值。
但是,您可以改進以下幾點:
SELECT *
。 無論當前場景是否需要它們,這都會帶入表的所有列。 此外,您將不確定列的順序。 而是指定查詢中需要的列名稱。 SqlCommand
,則僅需要獲取單個列(本例中為Name列ExecuteScalar()
時,請嘗試使用ExecuteScalar()
)。 這將使您不必運行Read()
循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.