Import hashtable's fields in a adodb recordset
Hi have a hashtable and an adodb.recordset. The field's names of hashtable are same of fields adodb.recordset How can i import the fields value of hashtable in fields adodb.recordset without do it manually?
ex
Dim Tot As ADODB.Recordset
Dim h As Hashtable开发者_运维知识库 = New Hashtable
h("a") = 1
h("b") = 2
h("d") = 4
the recordset tot has fields: "a", "b", "d"
i want import the hashtable values in recordset
thanks
Because i still don't know what the key/value of your HashTable are, i assume that there is only one record from db and the field-names(columns) of the record are the key and the value of these fields are the value in the HashTable for that key(fieldname).
This should work, although i'm afraid that your requirement is something else.
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
I provided a DataReader sample but the same works (hopefully) for an ADODB.Recordset(btw, why do you need such antiquated things?).
精彩评论