1

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

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

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

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

4

2 回答 2

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 和 Option Explicit。这将强制您声明 Description 和 WorkItemNumber 的类型,并在添加 ListItem 时将它们转换为字符串。这将减少运行时错误。

最后,如果这不是你正在做的一个小应用程序作为学习实验,你应该阅读分层应用程序设计。您的代码在同一方法中混合了 UI、业务逻辑和数据访问。这通常是不受欢迎的。

  • 您的“用户界面”LoadWorkItem() 方法应该向“核心”方法询问工作项列表。
  • 然后,您的核心方法应该询问数据的“数据访问”方法。
  • “数据访问”方法应该调用数据库。

快乐编码。

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

于 2013-09-24T11:33:57.530 回答
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
于 2017-02-17T19:09:23.960 回答