如何在vb.net的列表框中填充数据库中的项目

时间:2013-09-24 08:59:50

标签: vb.net

我正在使用oop概念开发一个应用程序。我有一个具有2个属性的类,并且有Get和Set方法,即WorkItemNumber和Description。

在客户端,我有一个列表框,用于根据描述填充工作项。这是我在课堂上写的代码o从数据库中读取项目。

Public Sub LoadWorkItem()
    ' Load the data.
    ' Select records.
    Dim oWorkItem As WorkItem = New WorkItem()
    Dim conn As New OleDbConnection
    Dim data_reader As OleDbDataReader
    conn = oWorkItem.GetDbConnection()
    Dim cmd As New OleDbCommand("SELECT * FROM work_item ORDER BY [work item number]", conn)
    data_reader = cmd.ExecuteReader()
    'ListBox1.Items.Clear()
    If data_reader.HasRows = True Then
        Do While data_reader.Read()
            WorkItemNumber = data_reader.Item("work item number")
            Description = data_reader.Item("description")
        Loop
    End If
    data_reader.Close()
    data_reader = Nothing
    cmd.Dispose()
    cmd = Nothing
    conn.Close()
    conn.Dispose()
End Sub

如何使用代码填充列表框,如果代码有任何改进,请告诉我。谢谢

2 个答案:

答案 0 :(得分:3)

要打开ListBox,请执行此操作...

ListBox1.Item.Clear()
If data_reader.HasRows Then
    Do While data_reader.Read()
        WorkItemNumber = data_reader.Item("work item number")
        Description = data_reader.Item("description")
        ListBox1.Items.Add(New ListItem(Description, WorkItemNumber)
    Loop
End If

就改进而言,首先使用Using语句进行数据库连接。在您的代码中,如果数据库连接打开时出现异常,它将永远不会关闭。这样更好......

Using conn As OleDbConnection = oWorkItem.GetDbConnection()
    ' Execute SQL and populate list... 
End Using

以上代码可确保您的连接已关闭。

然后,启用Option Strict and Option Explicit。这将强制您声明Type for Description和WorkItemNumber,并在添加ListItem时将它们强制转换为字符串。这将减少运行时错误。

最后,如果这只是一个小型应用程序,而您正在做一个学习实验,那么您应该阅读tiered application design。您的代码使用相同的方法混合UI,业务逻辑和数据访问。这通常是不受欢迎的。

  • 您的“用户界面”LoadWorkItem()方法应该询问“核心”方法以获取WorkItems列表。
  • 然后,您的核心方法应该询问数据的“数据访问”方法。
  • “数据访问”方法应该调用数据库。

快乐的编码。

更新:您可以在MSDN找到有关n层架构的精彩信息。一旦掌握了基础知识并对.NET有信心,一本好书就是Visual Basic .NET Business Objects

答案 1 :(得分:0)

    Imports System.Data.SqlClient 'Reference The Sql Client

    Public Class Form1
   ''Make sure to change the connection string below to your connection string this code only works for SQL DataBase. If Your connection String is wrong This will Not Work

    Dim connString As String = "Data         
    Source=NameofYourSQLServer\SQLEXPRESS;Initial Catalog=NameOfYourDataBase;Integrated Security=True"

Dim tblDIV As DataTable
Dim daDIV As SqlDataAdapter
Dim dsDIV As New DataSet
Dim oCon As SqlConnection

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim oCon = New SqlConnection
     oCon.ConnectionString = connString
     dsDIV = New DataSet

  ' Select all Fields and order by ID or Replace * with name of Field

   daDIV = New SqlDataAdapter("SELECT * FROM NameOfYourTable ORDER BY Id DESC", oCon)
    '*** Define command builder to generate the necessary SQL
    Dim builder As SqlCommandBuilder = New SqlCommandBuilder(daDIV)
    builder.QuotePrefix = "["
    builder.QuoteSuffix = "]"

    Try
        daDIV.FillSchema(dsDIV, SchemaType.Source, "DIV")
        daDIV.Fill(dsDIV, "DIV")
        tblDIV = dsDIV.Tables("DIV")
        ListBox1.DataSource = tblDIV
        ListBox1.DisplayMember = "NameOfTheFieldYouWanttoDisplay"
    Catch ex As Exception

        MsgBox("Encountered an Error;" & vbNewLine & ex.Message)

        oCon.Close()

    End Try
End Sub