有没有办法将数据从sql数据读取器值存储到数组?
这是我的代码
Dim machines() As Integer
Sub machine_entire()
Try
'getting lines from the database
If conn.State = ConnectionState.Closed Then conn.Open()
With cmd
.Connection = conn
.CommandText = "select machineID from tbl_prod_machine where lineID = '" & lineID & "' order by machineID asc"
End With
dr = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read
machines = {dr.GetInt32(dr.GetOrdinal("machineID"))}
End While
dr.Close()
End If
dr.Close()
Catch ex As DataException
MessageBox.Show(ex.Message, ex.GetType.ToString)
Catch ex As SqlException
MessageBox.Show("Sql Server Error # " & ex.Number & ": " & ex.GetType.ToString)
End Try
End Sub
谢谢你!寻求帮助
答案 0 :(得分:0)
如果它只是一个整数列表,我会使用List(Of)
Dim machines As New List(Of Integer)
之后,您只需将值添加到列表
machines.Add(dr.GetInt32(dr.GetOrdinal("machineID")))
旁注,我会重命名你的功能并让它返回列表。在这种情况下,lineId和机器似乎不适合在函数之外。
Function GetMachineIdsFromLine(ByVal lineId) As List(Of Integer)
此外,建议仅在需要时打开连接并尽快关闭它们。