如何使用 VisualBasic 中的 OLEDB 连接 SELECT 并存储具有相同 ID 的数据库的多行
How do I SELECT and store multiple rows of a database with the same ID using OLEDB Connection in VisualBasic
Private Sub GetAffectedTraits(ByVal QuestionIndex)
NumberOfTraitsAffected = 0
Dim IndexTracker As Integer
Dim TraitsAffected(23) As String
'Set all to Null value
For i As Integer = 1 To 23
TraitsAffected(i) = " "
Next
Dim Command As New OleDbCommand("SELECT [Affected Trait] FROM Sheet1 WHERE QuestionIndex=" & QuestionIndex & "", TraitValuesConn)
DB_Reader = Command.ExecuteReader
While DB_Reader.Read()
TraitsAffected(IndexTracker) = DB_Reader("Affected Trait")
IndexTracker += 1
End While
For i As Integer = 1 To 23
MsgBox(TraitsAffected(i)) 'Does not include affected traits yet for some reason
Next
End Sub
在此代码中,我尝试 select 并存储数据库中的每个 'Affected Trait',并使用相同的 ID(或 QuestionIndex)存储。我希望它们单独存储在 TraitsAffected 数组中。这是我目前正在尝试的,但它根本没有改变数组。我想坚持使用我在 OLEDB 连接中使用的语法。
这是一项学术作业。
如果您不确定受影响的特征数量,最好使用列表而不是数组。
检查以下代码,看看它是否适合您。
Private Sub GetAffectedTraits(ByVal QuestionIndex)
Dim NumberOfTraitsAffected = 0
Dim TraitsAffected As List(Of String) = New List(Of String)
Using TraitValuesConn As OleDbConnection = New OleDbConnection("your connection string")
TraitValuesConn.Open()
Dim Command As New OleDbCommand("SELECT [Affected Trait] FROM Sheet1 WHERE QuestionIndex=" & QuestionIndex & "", TraitValuesConn)
Dim DB_Reader = Command.ExecuteReader
While DB_Reader.Read()
TraitsAffected.Add(DB_Reader("Affected Trait"))
End While
End Using
For Each value As String In TraitsAffected
MsgBox(value)
Next
End Sub
Private Sub GetAffectedTraits(ByVal QuestionIndex)
NumberOfTraitsAffected = 0
Dim IndexTracker As Integer
Dim TraitsAffected(23) As String
'Set all to Null value
For i As Integer = 1 To 23
TraitsAffected(i) = " "
Next
Dim Command As New OleDbCommand("SELECT [Affected Trait] FROM Sheet1 WHERE QuestionIndex=" & QuestionIndex & "", TraitValuesConn)
DB_Reader = Command.ExecuteReader
While DB_Reader.Read()
TraitsAffected(IndexTracker) = DB_Reader("Affected Trait")
IndexTracker += 1
End While
For i As Integer = 1 To 23
MsgBox(TraitsAffected(i)) 'Does not include affected traits yet for some reason
Next
End Sub
在此代码中,我尝试 select 并存储数据库中的每个 'Affected Trait',并使用相同的 ID(或 QuestionIndex)存储。我希望它们单独存储在 TraitsAffected 数组中。这是我目前正在尝试的,但它根本没有改变数组。我想坚持使用我在 OLEDB 连接中使用的语法。
这是一项学术作业。
如果您不确定受影响的特征数量,最好使用列表而不是数组。
检查以下代码,看看它是否适合您。
Private Sub GetAffectedTraits(ByVal QuestionIndex)
Dim NumberOfTraitsAffected = 0
Dim TraitsAffected As List(Of String) = New List(Of String)
Using TraitValuesConn As OleDbConnection = New OleDbConnection("your connection string")
TraitValuesConn.Open()
Dim Command As New OleDbCommand("SELECT [Affected Trait] FROM Sheet1 WHERE QuestionIndex=" & QuestionIndex & "", TraitValuesConn)
Dim DB_Reader = Command.ExecuteReader
While DB_Reader.Read()
TraitsAffected.Add(DB_Reader("Affected Trait"))
End While
End Using
For Each value As String In TraitsAffected
MsgBox(value)
Next
End Sub