数据库:Microsoft Access创建的名为ConnStr的设置,带有值 提供商= Microsoft.ACE.OLEDB.12.0;数据源= c:\ sm.accdb产品 表有4列:ProductID,Description,Category,Price我有3个 课程:经理,产品,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 Class中!我感到很困惑!请帮帮我!
答案 0 :(得分:0)
您必须使用cmd.ExecuteReader()
方法
Dim myReader As OledbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While myReader.Read()
Console.WriteLine(myReader.GetString(0))
End While
myReader.Close()