1

我正在尝试制作一个列表框来显示 Access 数据库中特定表的所有字段。我有几张桌子,想法是让每张桌子都由不同的按钮加载(并清除框中的项目)。一个技巧是桌子的大小并不完全相同。如何让每个表的所有字段都显示出来。我现在只显示一个字段:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;data source='C:\dummy_data.accdb';"
    Dim conn As New OleDbConnection(connString)
    Dim sql As String = "SELECT * FROM Customers"
    Dim cmd As New OleDbCommand(sql, conn)
    conn.Open()
    Dim reader As OleDbDataReader = cmd.ExecuteReader()
    ClientList.Items.Clear()
    While reader.Read()
        ClientList.Items.Add(reader(0).ToString())
    End While
    reader.Close()
    conn.Close()

End Sub
4

2 回答 2

1

如果您不反对使用 aDataGridView而不是 a ListView,您可以这样做:

    Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source='PathToMyDatabase';"
    Dim sql As String = "SELECT * FROM Tメイン;"

    Dim dt As DataTable

    With New OleDbDataAdapter(sql, connString)
        Dim ds As New DataSet()
        .Fill(ds)
        dt = ds.Tables(0)
    End With

    Me.DataGridView1.DataSource = dt

我的计算机上没有“ACE.OLEDB”,因此必须改用 JET 提供程序,但它应该以相同的方式工作。

我应该补充一点,您可以使用此方法从数据库中检索数据,但据我所知,将 a 绑定DataTable到 a并没有简单的方法ListView(例如,请参阅此 MSDN 问题),您必须遍历列在您的DataTable第一个并将列标题添加到您的ListView,然后遍历行并添加数据。

更新:

为了回答您关于如何从数据库中导出数据的问题,DataGridView我记得我不久前编写了代码来执行此操作。

Private Function ExportToExcelFile(ByVal FileName As String) As Boolean

    With New Excel.Application
        With .Workbooks.Add()
            For Each sheet As Worksheet In .Worksheets
                If sheet.Index > 1 Then
                    Call sheet.Delete()
                End If
            Next
            With CType(.Worksheets(1), Worksheet).Range("A1")
                For Each column As DataGridViewColumn In Me.dgvData.Columns
                    .Offset(0, column.Index).Value = column.HeaderText
                    For Each row As DataGridViewRow In Me.dgvData.Rows
                        .Offset(row.Index + 1, column.Index).Value = row.Cells(column.Index).Value
                    Next
                Next
            End With
            Call .SaveAs(FileName)
        End With
        Call .Quit()
    End With

End Function

我希望这将有助于您入门。

于 2013-03-19T01:13:56.573 回答
1

使用 Listview 控件,下面的代码应该可以解决问题。为简单起见,我只定义了 4 个客户字段 - 您需要根据表字段定义来定义它们:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ClientList.View = View.Details
    ClientList.FullRowSelect = True
    ClientList.Columns.Add("ID", 120)
    ClientList.Columns.Add("Name", 120)
    ClientList.Columns.Add("Address", 140)
    ClientList.Columns.Add("Email", 100)
    Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\dummy_data.accdb;"
    ClientList.Items.Clear()
    Using conn As New Data.OleDb.OleDbConnection(connString)
        conn.Open()
        Dim sql As String = "SELECT * FROM Customers"
        Using cmd As New Data.OleDb.OleDbCommand(sql, conn)
            Dim lvi As ListViewItem
            Using oRDR As Data.OleDb.OleDbDataReader = cmd.ExecuteReader
                While oRDR.Read()
                    lvi = ClientList.Items.Add(oRDR.GetValue(0).ToString)
                    For i = 1 To oRDR.FieldCount - 1
                        lvi.SubItems.Add(oRDR.GetValue(i).ToString)
                    Next
                End While
            End Using
        End Using
        conn.Close()
    End Using

End Sub
于 2013-03-19T01:28:55.457 回答