如何使用相同的代码从 DataRow 或 DataReader 中读取数据?

发布于 2024-12-08 12:32:41 字数 822 浏览 3 评论 0原文

目前,我使用的 SqlDataReader 加载了一个简单的数据库“选择”查询。每行代表一个域对象,因此我像这样膨胀每个对象:

Dim loadedItems As New List(Of Item)
Dim dr As SqlDataReader = GetItemsDataReader()
While dr.Read()
  Dim item As Item = GetItemFromData(dr)
  loadedItems.Add(item)
End While

我编写的 GetItemFromData 方法看起来像这样:

Private Function GetItemFromData(dr As SqlDataReader) As Item
  Dim loadedItem As New Item()
  loadedItem.ID = dr("ID")
  loadedItem.Name = dr("Name")
  'etc., etc.'
  Return loadedItem
End Function

在某些情况下,我必须从 DataRow 读取数据> 而不是 SqlDataReader。但代码是完全相同的!当我查看 GetItemFromData 方法时,我想在 dr 参数中接受更通用的对象类型,以便我可以处理 DataReaderDataRow 相同,因为如果我编写了一个要使用 DataRow 的代码,我将在该方法中编写完全相同的代码。有办法做到这一点吗?

Currently, I am using a SqlDataReader loaded up with a simple database 'Select' query. Each row represents a domain object, so I inflate each object like this:

Dim loadedItems As New List(Of Item)
Dim dr As SqlDataReader = GetItemsDataReader()
While dr.Read()
  Dim item As Item = GetItemFromData(dr)
  loadedItems.Add(item)
End While

The GetItemFromData method I wrote looks something like this:

Private Function GetItemFromData(dr As SqlDataReader) As Item
  Dim loadedItem As New Item()
  loadedItem.ID = dr("ID")
  loadedItem.Name = dr("Name")
  'etc., etc.'
  Return loadedItem
End Function

In some cases, I have to read data from a DataRow instead of a SqlDataReader. But the code would be the exact same! As I look at my GetItemFromData method, I want to accept a more generic type of object in the dr parameter so that I can treat a DataReader the same as a DataRow, since I would be writing the exact same code inside the method if I wrote one that was meant to use a DataRow. Is there a way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅窗月明清似水 2024-12-15 12:32:41

我能想到的唯一方法是包装一些类并实现一个接口 - 类似于:

Interface IIndexer
    Default ReadOnly Property Item(ByVal index As String)
End Interface

Class DataReaderWrapper
    Implements IIndexer

    Private ReadOnly _reader As IDataReader

    Public Sub New(reader As IDataReader)
        _reader = reader
    End Sub

    Public ReadOnly Property Item(index As String) As Object Implements IIndexer.Item
        Get
            Return _reader(index)
        End Get
    End Property
End Class

Class DataRowWrapper
    Implements IIndexer

    Private ReadOnly _row As DataRow

    Public Sub New(row As DataRow)
        _row = row
    End Sub

    Public ReadOnly Property Item(index As String) As Object Implements IIndexer.Item
        Get
            Return _row(index)
        End Get
    End Property
End Class

然后,您必须在将行或读取器传递给您的方法之前包装您的行或读取器:

Private Function GetItemFromData(indexer As IIndexer) As Item
    Dim loadedItem As New Item()
    loadedItem.ID = indexer("ID")
    loadedItem.Name = indexer("Name")
    'etc., etc.'
    Return loadedItem
End Function

Dim i As Item = GetItemFromData(New DataRowWrapper(dr))

Only way I can think of off the top of my head would be to wrap a few classes and implement an inferface - something like:

Interface IIndexer
    Default ReadOnly Property Item(ByVal index As String)
End Interface

Class DataReaderWrapper
    Implements IIndexer

    Private ReadOnly _reader As IDataReader

    Public Sub New(reader As IDataReader)
        _reader = reader
    End Sub

    Public ReadOnly Property Item(index As String) As Object Implements IIndexer.Item
        Get
            Return _reader(index)
        End Get
    End Property
End Class

Class DataRowWrapper
    Implements IIndexer

    Private ReadOnly _row As DataRow

    Public Sub New(row As DataRow)
        _row = row
    End Sub

    Public ReadOnly Property Item(index As String) As Object Implements IIndexer.Item
        Get
            Return _row(index)
        End Get
    End Property
End Class

You would then have to wrap your row or reader before passing it to your method:

Private Function GetItemFromData(indexer As IIndexer) As Item
    Dim loadedItem As New Item()
    loadedItem.ID = indexer("ID")
    loadedItem.Name = indexer("Name")
    'etc., etc.'
    Return loadedItem
End Function

Dim i As Item = GetItemFromData(New DataRowWrapper(dr))
不奢求什么 2024-12-15 12:32:41

如何使用 DataTable 代替:

Dim DataTable table AS new DataTable()
table.Load(GetItemsDataReader())
foreach (DataRow row in table.Rows) {
   Dim item As Item = GetItemFromData(row)
   loadedItems.Add(item)
}

更改 GetItemFromData() 方法以采用 DataRow 而不是 DataReader(对于 VB.NET/ C# 混合伪代码)。

How about using a DataTable instead:

Dim DataTable table AS new DataTable()
table.Load(GetItemsDataReader())
foreach (DataRow row in table.Rows) {
   Dim item As Item = GetItemFromData(row)
   loadedItems.Add(item)
}

Change your GetItemFromData() method to take a DataRow instead of a DataReader (sorry about the VB.NET/C# hybrid pseudo code).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文