【问题标题】:How to get product by its ID?如何通过 ID 获取产品?
【发布时间】: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。我想知道它是否有效!

标签: vb.net select


【解决方案1】:

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

Dim myReader As OledbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    While myReader.Read()
        Console.WriteLine(myReader.GetString(0))
    End While
    myReader.Close()

【讨论】:

  • ExecuteDataReader 不是 System.Data.OleDb.OleDbCommand 的成员
  • 我很乐意帮助你,乔
猜你喜欢
  • 2017-03-30
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2023-01-28
  • 1970-01-01
相关资源
最近更新 更多