0

数据库:Microsoft Access 创建了名为 ConnStr 的设置,值为 provider=Microsoft.ACE.OLEDB.12.0; Data Source=c:\sm.accdb 产品表有4列:ProductID、Description、Category、Price 我有3个类:Manager、Product、ProductManager

Public MustInherit Class Manager
    Private _connectionString As String
    Protected Property ConnectionString() As String
        Get
            Return _connectionString
        End Get
        Set(ByVal value As String)
            _connectionString = value
        End Set
    End Property

    Public Sub New(ByVal connStr As String)
        ConnectionString = connStr
    End Sub
End Class

Public Class Product
    Private _id As Integer
    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Private _description As String
    Public Property Description() As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
        End Set
    End Property

    Private _category As String
    Public Property Category() As String
        Get
            Return _category
        End Get
        Set(ByVal value As String)
            _category = value
        End Set
    End Property

    Private _price As Double
    Public Property Price() As Double
        Get
            Return _price
        End Get
        Set(ByVal value As Double)
            _price = value
        End Set
    End Property
End Class

    Imports System.Data.OleDb
    Public Class ProductManager
Inherits Manager
        Public Function GetProductByID(ByVal id As Integer) As Product
                Dim con = New System.Data.OleDb.OleDbConnection
                Dim sql As String = "SELECT * FROM Product WHERE ProductID=@id"
                con.Open()
                Try
                    Dim description As String
                    Dim category As String
                    Dim price As Double
                    Dim cmd As New System.Data.OleDb.OleDbCommand(sql, con)
                    cmd.Parameters.Add(id.ToString, "@id")
                    cmd.Parameters.Add(description, "@description")
                    cmd.Parameters.Add(category, "@category")
                    cmd.Parameters.Add(price.ToString, "@price")
                    cmd.ExecuteNonQuery()
                    cmd.Dispose()
                    cmd = Nothing
                Catch ex As Exception
                    Throw New Exception(ex.ToString(), ex)
                Finally
                    con.Close()
                End Try
                Return nothing
        End Function
    End Class

我无法从数据库中获取产品!我认为问题出在 ProductManager 类中!我感到很困惑!请帮我!

4

1 回答 1

0

你必须使用cmd.ExecuteReader()方法

Dim myReader As OledbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    While myReader.Read()
        Console.WriteLine(myReader.GetString(0))
    End While
    myReader.Close()
于 2013-04-20T18:33:29.170 回答