Jump to content
Razvanvb

Mostrar informação da base de dados numa listbox

Recommended Posts

Razvanvb

Boas pessoal.

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

Quero fazer algo como isto:

http://codedisplay.com/wp-content/uploads/2014/10/How-to-Bind-MS-Access-Database-Table-to-ListBox-Control-ASP.NET-Csharp-VB.Net_.png

Mostrar esses nomes.

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

       Dim dt As DataTable

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

Se alguém puder ajudar, agradeço!

Share this post


Link to post
Share on other sites
vikcch

Boas,

Experimenta assim:

Try
	    Using conn = New OleDbConnection(connString)
		    conn.Open()
		    '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
						    'ou
						    'Me.ListBox1.Items.Add(reader.Item("nome do campo"))
					    End While
				    Else
					    MessageBox.Show("Sem dados")
				    End If

			    End Using
		    End Using
	    End Using

    Catch ex As Exception
	    MessageBox.Show(ex.ToString)
    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.