Jump to content

Mostrar informação da base de dados numa listbox

Recommended Posts


Boas pessoal.

Eu queria mostrar informação da base de dados que tenho (ACCESS) e mostrar numa listbox.

Quero fazer algo como isto:


Mostrar esses nomes.

Eu tentei fazer com este código, mas nada aparece:

       Dim dt As DataTable

       Using con
           Dim cmd As New OleDbCommand("SELECT * FROM Livrosescola", con)
           Dim oDA As New OleDbDataAdapter(cmd)
           dt = New DataTable()
       End Using
       ListBox1.DataSource = dt

Se alguém puder ajudar, agradeço!

Share this post

Link to post
Share on other sites


Experimenta assim:

	    Using conn = New OleDbConnection(connString)
		    'Se só precisas de um campo, é melhor só puxares esse.... no lugar do *
		    Dim query As String = "SELECT [nome do campo] FROM Livrosescola"

		    Using command = New OleDbCommand(query, conn)

			    Using reader As OleDbDataReader = command.ExecuteReader()

				    If reader.HasRows Then
					    While (reader.Read())
						    Me.ListBox1.Items.Add(reader.GetValue(0)) ' Zero é o index do campo
						    'Me.ListBox1.Items.Add(reader.Item("nome do campo"))
					    End While
					    MessageBox.Show("Sem dados")
				    End If

			    End Using
		    End Using
	    End Using

    Catch ex As Exception
    End Try

  • Vote 1

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.