2015-01-14 85 views
0

好像是什麼存儲多維數據vb.net存儲多維數據

當我從數據庫中檢索記錄的最簡單的方法我想知道最簡單的方法,我用一個數組來存儲,我會用一些信息後來這樣的

rowCtr = 0 
For Each dr As DataRow In dt.Rows 
    md(rowCtr, 0) = dr("member_id").ToString 
    md(rowCtr, 1) = dr("full_name").ToString 
    md(rowCtr, 2) = dr("status").ToString 
    md(rowCtr, 3) = dr("archived").ToString 

    ... 

    rowCtr = rowCtr + 1 
Next 

訪問特定成員的數據,我用這個

'first i loop through the md array to get the array index (ind) of a member 
'then, i can get the data of a member by using this 
md(ind, 0) 'To get the id 
md(ind, 1) 'To get the full name 

它有點困難,因爲我總是需要知道並指定索引

我希望它是這樣

md("443", "full_name") 'to get the full name 
md("443", "status") 'to get the status 

,其中443是其成員的ID,我用它作爲第一個維度

我看了一下哈希表,字典,列表的關鍵 - 但我似乎無法找到在多維度的風格

如果可能的話使用它們一個很好的例子,我也想長度是動態的,而當我刪除索引,其餘的將填補它的現貨 -

我還需要它有一個搜索方法,以查找member_id是否已經在列表中

最簡單的方法是什麼?請回復..謝謝

回答

2

不要打擾將數據複製到陣列 - 只使用DataTable直接:

Dim drMatch() As DataRow = dt.Select("member_id='443'") 
If drMatch.GetUpperBound(0) >= 0 Then 
    MsgBox(drMatch(0).Item("full_name").ToString) 
End If 

如果是組裝不同來源的數據進入你的陣列,我會定義一個新的內存DataTable使用,而不是你的陣列。

+0

謝謝,夥計我會嘗試 – jks

1

,如果你必須把它保存爲一個數組(不知道爲什麼你會)你可以使用ENUM:

Enum Table1 
    Member_ID = 1 
    Full_Name = 2 
    Status = 3 
    archived = 4 
End Enum 

那麼你的數組中,你可以這樣做:

md("443", Table1.Full_Name) 'to get the full name 
    md("443", Table1.Status) 'to get the status 
+0

o,謝謝你告訴我關於Enum,我可能會在未來使用它.. – jks