从SQLite数据库获取对象

时间:2014-02-15 04:08:08

标签: vb.net sqlite

我一直在浏览sqlite的文档,我无法弄清楚如何将查询返回到对象中。我可以迭代。这是我一直在玩的代码。我希望有人能指出我正确的方向。提前谢谢。

    Private Sub Query(ByVal SQLString As String)
        Try
            Dim SQLiteDRObj As SQLiteDataReader
            Dim ResultsObj As DataTable = Nothing
            Dim ResultSet As DataSet = Nothing
            Dim SQLAdapter As SQLiteDataAdapter = Nothing

            Me.GetSqlConnection()
            SQLConnection.Open()
            SQLCommand = New SQLiteCommand(SQLConnection)
            SQLCommand.CommandText = SQLString

            'SQLAdapter = New SQLiteDataAdapter("SELECT * FROM Joes2", SQLConnection)
            'SQLAdapter.MissingMappingAction = MissingMappingAction.Ignore
            'SQLAdapter.Fill(ResultSet, "Joes2")

            SQLiteDRObj = SQLCommand.ExecuteReader()

            'Put the results into an object ????

            SQLiteDRObj.Close()
            SQLConnection.Close()
            SQLConnection.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

EDITED

假设我有一个这样的课程

Public Class Drawing
    Private _ID As Guid
    Private _DrawingDate As Date
    Private _GameID As Guid

    Public Property ID As Guid
        Get
            Return _ID
        End Get
        Set(ByVal value As Guid)
            _ID = value
        End Set
    End Property

    Public Property DrawingDate As Date
        Get
            Return _DrawingDate
        End Get
        Set(ByVal value As Date)
            _DrawingDate = value
        End Set
    End Property

End Class

我在SQLite中有一个表,比如这个......

Table Name is "Drawings"
ID, DrawingDate
(SomeGuid, 2014-2-14)
(SomeGUid, 2014-2-15)

现在我想加载它。

Dim Drawings as New Drawing("SELECT * From Drawings")

如果这是完全错误的方法,请告诉我。我只是想坚持适当的OOP。 (试图以正确的方式学习)

1 个答案:

答案 0 :(得分:1)

这个问题不是针对SQLite的。无论提供者还是数据源,ADO.NET的工作方式基本相同。究竟是什么意思“成为一个对象”?如果要使用结果集填充DataTable,则创建一个并调用其Load方法,将数据读取器作为参数传递。如果您的意思是要创建实体类的实例,那么您将使用Do或While循环,在数据读取器上调用Read并按名称或序数获取每个字段值。