1

我是 VB 的新手。我有我的类 ( dalv1),我的所有函数都存储在其中,还有我的主窗体。

代码如下:

Public Sub getCustomerDetails()

        Try
            Dim cust_dataTable As New DataTable()
            Dim Connection As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Users\Public\Documents\VP - A1\Database\Database.mdb"
            Dim SQL As String = ("SELECT * FROM tblCustomers")
            Dim dataAdapter As New OleDbDataAdapter(SQL, Connection)
            dataAdapter.Fill(cust_dataTable)
            dataAdapter.Dispose()

        Catch ex As Exception
            MsgBox("ERROR : " & ex.Message.ToString)
        End Try

End Sub

我想将 DataTable 传递cust_dataTable给主窗体,以便在窗体加载时填充文本框。

4

1 回答 1

2

您需要将其转换为函数,以便将表返回给调用者:

Public Function getCustomerDetails() As DataTable
  Dim cust_dataTable As New DataTable()
  Try
    Dim Connection As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Users\Public\Documents\VP - A1\Database\Database.mdb"
    Dim SQL As String = ("SELECT * FROM tblCustomers")
    Dim dataAdapter As New OleDbDataAdapter(SQL, Connection)
    dataAdapter.Fill(cust_dataTable)
    dataAdapter.Dispose()        
  Catch ex As Exception
    MsgBox("ERROR : " & ex.Message.ToString)
  End Try

  Return cust_dataTable
End Function

您没有共享表单的代码,但这里有一个如何调用它的示例:

Private da As New dalv1
Private _MyDataTable As DataTable

Protected Overrides Sub OnLoad(ByVal e As EventArgs)
  MyBase.OnLoad(e)
  _MyDataTable = da.getCustomerDetails
End Sub
于 2012-11-05T20:46:44.817 回答