在VB.NET中将DataReader的结果存储到数组中
发布时间:2020-12-17 07:22:51 所属栏目:百科 来源:网络整理
导读:如何将 DataReader的结果存储到数组中,但仍能按列名引用它们?我本质上希望能够克隆DataReader的内容,以便我可以关闭阅读器并仍然可以访问.我不想像所有人建议的那样将物品存放在 DataTable中. 我已经看到了很多答案,但我找不到任何我想要的东西 解决方法 我
如何将
DataReader的结果存储到数组中,但仍能按列名引用它们?我本质上希望能够克隆DataReader的内容,以便我可以关闭阅读器并仍然可以访问.我不想像所有人建议的那样将物品存放在
DataTable中.
我已经看到了很多答案,但我找不到任何我想要的东西 解决方法
我发现这样做最简单的方法是使用字符串填充数组,其中字符串为键,对象为值,如下所示:
' Read data from database Dim result As New ArrayList() Dr = myCommand.ExecuteReader() ' Add each entry to array list While Dr.Read() ' Insert each column into a dictionary Dim dict As New Dictionary(Of String,Object) For count As Integer = 0 To (Dr.FieldCount - 1) dict.Add(Dr.GetName(count),Dr(count)) Next ' Add the dictionary to the ArrayList result.Add(dict) End While Dr.Close() 所以,现在你可以使用for循环遍历结果,如下所示: For Each dat As Dictionary(Of String,Object) In result Console.Write(dat("ColName")) Next 非常类似于如果它只是DataReader你会怎么做: While Dr.Read() Console.Write(Dr("ColName")) End While 此示例使用MySQL / NET驱动程序,但相同的方法可以与其他流行的数据库连接器一起使用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |