在adodb记录集中导入哈希表的字段

时间:2010-10-26 10:08:06

标签: vb.net hashtable adodb

你有一个哈希表和一个adodb.recordset。 哈希表的字段名称与字段adodb.recordset相同 如何在adodb.recordset字段中导入哈希表的字段值而不手动执行?

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

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

记录集包含字段:“a”,“b”,“d”

我想在记录集中导入哈希表值

感谢

1 个答案:

答案 0 :(得分: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(btw,你为什么需要这些过时的东西?)。

相关问题