2010-10-26 65 views
0

嗨有一個哈希表和adodb.recordset。 該字段的哈希表的名稱是相同的字段adodb.recordset 我怎樣才能在字段adodb.recordset字段值的哈希表沒有手動做?在adodb記錄集中導入哈希表的字段

Dim Tot As ADODB.Recordset 
Dim h As Hashtable = New Hashtable 

h("a") = 1 
h("b") = 2 
h("d") = 4 

記錄TOT具有字段: 「A」, 「B」, 「d」

我想記錄導入的哈希值

感謝

+0

您想根據數據庫中的記錄更新散列表,其中列是鍵,數據是值? – 2010-10-26 10:13:05

+0

你在Hashtable中的關鍵和價值是什麼? – 2010-10-26 10:29:43

+0

我已經舉了一個例子 – 2010-10-26 10:31:52

回答

0

因爲我仍然不知道你的HashTable的鍵/值是什麼,我假設只有一個來自db的記錄,記錄的字段名(列)是鍵和這些值字段是該鍵(字段名)的HashTable中的值。

這應該工作,雖然我怕你的需求是別的。

Dim hash As New Dictionary(Of String, Object)'this is your "HashTable"' 
    Dim con As New SqlClient.SqlConnection(My.Settings.SQLSERV2_RM2) 
    Try 
     Using con 
      Using command As New System.Data.SqlClient.SqlCommand("SELECT idClaimStatus, ClaimStatusName FROM dimClaimStatus ORDER BY ClaimStatusName", con) 
       command.Connection.Open() 
       Using reader As System.Data.SqlClient.SqlDataReader = command.ExecuteReader 
        While reader.Read 
         For i As Int32 = 0 To reader.FieldCount - 1 
          Dim field As String = reader.GetName(i) 
          If Not hash.ContainsKey(field) Then 
           hash.Add(field, reader(i)) 
          Else 
           hash(field) = reader(i) 
          End If 
         Next 
        End While 
       End Using 
      End Using 
     End Using 
    Catch ex As Exception 
     Throw 
    Finally 
     'nothing to do here because using closes the connection automatically' 
    End Try 

我提供一個DataReader樣品,但相同的作品(希望)爲ADODB.Recordset(順便說一句,你爲什麼需要這樣的過時的東西?)。