【发布时间】:2013-04-21 02:54:29
【问题描述】:
数据库:Microsoft Access 创建了名为 ConnStr 的设置,其值为 提供者=Microsoft.ACE.OLEDB.12.0;数据源=c:\sm.accdb 产品 表有 4 列:ProductID、Description、Category、Price 我有 3 类:经理、产品、产品经理
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 类中!我感到很困惑!请帮帮我!
【问题讨论】:
-
是有错误还是什么?你必须比这更具体。
-
你的参数和
cmd.ExecuteNonQuery有问题!是选择吗? -
我应该更改 cmd.ExecuteNonQuery 吗?
-
JohnFX,现在我在后端工作。我从创建产品功能中复制了这段代码,并尝试修改以使其成为 getproductid。我想知道它是否有效!