从数据库创建列表框值

Create Listbox Values from Database

这是我第二次使用 VB.net,所以我不确定语言。 我正在尝试从标题为 "category" 的 table 列中创建列表框值。有重复项,所以我试图只显示每个类别一次。我必须这样做,因为用户可以添加更多类别,这意味着我的列表框值需要动态更新。

我不确定我是否正确执行此过程:

Protected Sub CategoryListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CategoryListBox.SelectedIndexChanged

    Dim conn As OleDbConnection = New OleDbConnection("Provider=""*******"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)

    Dim ClassifiedStr As OleDbCommand = New OleDbCommand("SELECT classid, Addate, username, category, description FROM t_classifieds", conn)
    Dim OracleDataAdapterAds As OleDbDataAdapter = New OleDbDataAdapter
    OracleDataAdapterAds.SelectCommand = ClassifiedStr
    Dim DsAds As DataSet = New DataSet
    DsAds.Clear()
    OracleDataAdapterAds.Fill(DsAds, "t_classifieds")

    CategoryListBox.DataTextField = "category"
    CategoryListBox.DataValueField = "category"
    CategoryListBox.DataSource = DsAds
    CategoryListBox.DataMember = "t_classifieds"
    CategoryListBox.DataBind()

    Dim Categories As String
    Dim CategoryID

    For Each dr As DataRow In DsAds.Tables("t_classifieds").Rows
        Categories = dr("category").ToString()
        CategoryID = dr("classid").ToString()

        CategoryListBox.Items.Add(Categories)

    Next dr

    conn.Close()
End Sub

我查看了其他示例并尝试对它们进行镜像,但我的列表框中没有打印任何内容。

如果我使用预定义的 Dim 值,我的代码的第二个版本可以工作,但由于某种原因,我没有获得数据库连接来显示数据库类别列表。

第二个版本:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    FillList()

End Sub

Private Sub FillList()

    Dim conn As OleDbConnection = New OleDbConnection("Provider=""MSDAORA.1"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)
    Dim data_reader As OleDbDataReader
    Dim ClassifiedStr As OleDbCommand = New OleDbCommand("SELECT * FROM t_classifieds ", conn)
    Dim Categories
    conn.Open()
    data_reader = ClassifiedStr.ExecuteReader()
    CategoryListBox.Items.Clear()
    If data_reader.HasRows Then
        Do While data_reader.Read()
            Categories = data_reader.Item("category")
            'CategoryListBox.Items.Add(New ListItem(Categories))
            CategoryListBox.Items.Add(Categories.ToString())
        Loop
    End If
    data_reader.Close()
    data_reader = Nothing
    ClassifiedStr.Dispose()
    ClassifiedStr = Nothing
    conn.Close()
    conn.Dispose()


    'Dim dWorkDate As Date = CDate("01.01.2014")
    'While dWorkDate < Date.Today
    'CategoryListBox.Items.Add(dWorkDate.ToString("dd.MM.yyyy"))
    'dWorkDate = dWorkDate.AddDays(1)
    'End While
End Sub

Select DISTINCT 类别值:

Dim ClassifiedStr As OleDbCommand = New OleDbCommand("SELECT DISTINCT category FROM t_classifieds", conn)

'...

CategoryListBox.DataSource = DsAds.Tables("t_classifieds")
CategoryListBox.DataTextField = "category"
CategoryListBox.DataValueField = "category"

顺便说一下,您不需要在 New DataSet 上调用 Clear

意识到我的旧代码有效,但我需要@Keith Sql 代码。

If Not IsPostBack Then
        If Not CategoryListBox Is Nothing Then


            Dim conn As OleDbConnection = New OleDbConnection("Provider=""****"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)

            'Display All Classified Ads listed in the Database based on the following format/order
            'Date    Name    Home Phone Number     Description

            Dim ClassifiedStr As OleDbCommand = New OleDbCommand("SELECT DISTINCT category FROM t_classifieds", conn)
            Dim OracleDataAdapterAds As OleDbDataAdapter = New OleDbDataAdapter
            OracleDataAdapterAds.SelectCommand = ClassifiedStr
            Dim DsAds As DataSet = New DataSet
            DsAds.Clear()
            OracleDataAdapterAds.Fill(DsAds, "t_classifieds")
            CategoryListBox.DataSource = DsAds
            CategoryListBox.DataMember = "t_classifieds"
            CategoryListBox.DataBind()
        End If
    End If

我唯一的问题是随机 space:/