[英]How to get a specific data from mysql database vb.net
My code searches for the same value of the selected item of a listview in the database for example: "01" the code searches for "01" in the database, now 01 in the database is equivalent to a name for example 01 = Name, my problem is; 我的代码在数据库中搜索列表视图中所选项目的相同值,例如:“ 01”,代码在数据库中搜索“ 01”,现在数据库中的01等同于名称,例如01 = Name,我的问题是 If the code founds 01 i want to get the name instead of the 01. 如果代码找到01,我想获取名称而不是01。
My Code 我的密码
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
Although your code has many issues, the direct answer to your question is that you should call Reader.GetString(ColumnNumber)
inside the While
loop to get the value of Name
column. 尽管您的代码有很多问题,但直接的问题答案是您应该在While
循环内调用Reader.GetString(ColumnNumber)
以获取Name
列的值。
However, there are a few things you could improve: 但是,您可以改进以下几点:
SELECT *
in your queries. 不要在查询中使用SELECT *
。 This will bring in all of the columns of your table whether you need them in the current scenario or not. 无论当前场景是否需要它们,这都会带入表的所有列。 Also, you'll not be sure of the order of the columns. 此外,您将不确定列的顺序。 Instead specify the column names you need in your query. 而是指定查询中需要的列名称。 SqlCommand
in your code, try using ExecuteScalar()
if you simply need to fetch a single column (Name column in your case). 如果您倾向于直接在代码中使用SqlCommand
,则仅需要获取单个列(本例中为Name列ExecuteScalar()
时,请尝试使用ExecuteScalar()
)。 That will save you from having to run the Read()
loop. 这将使您不必运行Read()
循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.