希望在這件事上有所幫助。標題幾乎解釋了我正在嘗試做的事情。
我正在使用 MySql 資料庫從用戶 ID 中讀取他們所購買的資料,但是我遇到了困難,因為我一直堅持如何讀取具有相同 ID 的多行。
例如
1, TestProduct
1, TestProduct2
^^^ 由于有更多行填充了相同的 ID,我如何讀取多行?
這就是我目前正在做的事情,我知道這不起作用,因為它只是獲取/查找它找到的第一個 ID 結果并使用該結果,但是我不需要填充多行。所以我不知所措
SearchUser_COMMAND.Parameters.Add("@userid", MySqlDbType.VarChar).Value = Lbl_UserID.Text
Dim reader2 As MySqlDataReader
reader2 = SearchUser_COMMAND.ExecuteReader()
If reader2.Read() Then
Lbl_Active.Text = reader2(3)
Lbl_ProductName.Text = reader2(2)
Lbl_ProductExpire.Text = reader2(6)
End If
對此問題的任何幫助將不勝感激。
非常感謝提前
uj5u.com熱心網友回復:
您可以創建一個類來保存您的資料,填充這些物件的串列,然后使用一些 LINQ 來迭代它們。
Private Class Data
Public Property Active As Boolean
Public Property Name As String
Public Property Expire As DateTime
End Class
Dim items As New List(Of Data)
If reader2.HasRows Then
While reader2.Read()
items.Add(New Data() With {.Name = CStr(reader2(2)), .Active = CBool(reader2(3)), .Expire = CDate(reader2(6))})
End While
Lbl_Active.Text = String.Join(Environment.NewLine, items.Select(Function(i) i.Active))
Lbl_ProductName.Text = String.Join(Environment.NewLine, items.Select(Function(i) i.Name))
Lbl_ProductExpire.Text = String.Join(Environment.NewLine, items.Select(Function(i) i.Expire))
End If
' maybe clear labels otherwise
對于具有三個專案的讀者,這應該會導致類似這樣的結果
Lbl_Active:
真真 真
_
Lbl_產品名稱:
名稱 1 名稱
2 名稱
3
Lbl_ProductExpire:
日期 1 日期2
日期
3
我冒昧地根據名稱假設資料型別。您可能在資料庫中有所有字串(您不應該),但是您應該使用字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/520131.html
標籤:VB.net表格
