1

我正在寻求有关如何在查询中返回多行的帮助。例如,如果我想访问列的全部内容,我想将所有行返回到 ListBox。

Imports System.Data.OleDb

Public Class Form1

    Dim theConnectionString As New OleDbConnection

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        txtSQL.Clear()
        theResults.Items.Clear()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        theConnectionString.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Marc Wilson\Documents\FiddleFuckDB.accdb"
        theConnectionString.Open()
        Dim theDataSet As New DataSet
        Dim theDataTable As New DataTable
        theDataSet.Tables.Add(theDataTable)
        Dim theDataAdapter As New OleDbDataAdapter

        Dim theSQLStatement As String
        Dim theDBCommand As New OleDbCommand
        theSQLStatement = txtSQL.Text


        Try
            theDataAdapter = New OleDbDataAdapter("SELECT [City] FROM PI", theConnectionString)
            theDataAdapter.Fill(theDataTable)
            Dim theRowCount As Integer = theDataTable.Rows.Count
            Dim theItemCount As Integer = theDataTable.Columns.Count
            MessageBox.Show("Row Count: " & theRowCount & vbNewLine & "Item Count: " & theItemCount, "Your Results...")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

        Try
            theDBCommand = New OleDbCommand(theSQLStatement, theConnectionString)
            theResults.Items.Add(theDataTable.Rows(0).Item(0))
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        theConnectionString.Close()
    End Sub
End Class

我使用的 SQL 语句是SELECT [City] FROM PI(我有 3 行数据和 4 个不同的列)我相信我的错误在于这部分 theResults.Items.Add(theDataTable.Rows(0).Item(0))

4

2 回答 2

1

您尚未显示txtSQL.Text您尝试使用的 sql 语句。

一个简单的SELECT语句将从表中检索所有行,除非使用WHERE子句来限制返回的行数。

于 2013-06-05T01:31:15.237 回答
0

像这样试试…………

Dim x as Integer

Try

   theDataAdapter = New OleDbDataAdapter("SELECT [City] FROM PI", theConnectionString)
   theDataAdapter.Fill(theDataTable)

   For x = 0 to theDataTable.Rows.Count -1
      theResults.Items.Add(theDataTable.Rows(x).Item(0))
   Next

Catch ex As Exception

   MessageBox.Show(ex.ToString)

End Try
于 2013-06-05T02:01:11.183 回答