Vb.Net - 使用多维数组

时间:2013-08-14 05:00:06

标签: vb.net

请帮助一下 - 以前我把它存放在数据库中

File Name - Order.csv 
File Arrival Time - 03:00
File Arrival End time - 04:00

但现在我需要从数据库中移出它并使用MULTI-DIMENSIONAL ARRAY存储值,然后遍历它们以检查文件是否已到达。

我有代码来检查文件是否存在但是我对vb.net不熟悉所以现在不要将它存储在多维数组中然后遍历它。

请帮忙。

1 个答案:

答案 0 :(得分:2)

如果您真的想使用数组,可以尝试类似:

Dim data(,,) As String
ReDim data(10, 10, 10) ' three dimensions
For i As Integer = 0 To data.GetUpperBound(0)
    For j As Integer = 0 To data.GetUpperBound(1)
        For k As Integer = 0 To data.GetUpperBound(2)
           ' do your stuff here
        Next
    Next
Next

但是,为什么不使用对象和列表呢?类似的东西:

Public Class MyFile
    Property Name As String
    Property ArrivalTime As DateTime
    Property EndTime As DateTime
End Class

' then store the data in a list(of MyFile):
Public Class Test
    Sub LoadingData()
        Dim myData = New List(Of MyFile)
        Using conn As New SqlClient.SqlConnection("your connection string here")
            Using cmd As New SqlClient.SqlCommand()
                cmd.Connection = conn
                cmd.CommandText = "SELECT top 100 Name, ArrivalTime, EndTime FROM yourTable"
                conn.Open()
                Using rdr As SqlClient.SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                    Dim namePos = rdr.GetOrdinal("Name")
                    Dim timePos = rdr.GetOrdinal("Time")
                    Dim endTimePos = rdr.GetOrdinal("EndTime")
                    While rdr.Read()
                        myData.Add(New MyFile With {.Name = rdr.GetString(namePos),
                                                   .ArrivalTime = rdr.GetDateTime(timePos),
                                                   .EndTime = rdr.GetDateTime(endTimePos)})
                    End While
                End Using
            End Using
        End Using

        For Each item In myData
            ' Do whatever you like with each item
            Console.WriteLine(String.Format("Name: {0}, Arrival Time: {1:yyyy-MM-dd HH:mm:ss}, End Time: {2:yyyy-MM-dd HH:mm:ss}",
                                            item.Name,
                                            item.ArrivalTime,
                                            item.EndTime))
        Next
    End Sub
End Class

- 只是一个快速的样本,没有经过测试,你可以根据你的需要进行调整)......