retrieving data in VB from SQL


I use Visual Basic 2010 and Microsoft SQL Server 2008. I have my database and my table and i made the connection (at least i think i did) in VB using only the interface.

What i want to know is how to get data from the database and use it into my VB project. I have of course searched for solutions already but the differences i find only confuse me more. What i need to know are the basics, the tools/objects and procedures to retrieve the data.

What i try to do at the moment is make a simple selection and put that data into a listbox right when the program starts, like this:

Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load SqlConnection1.Open() SqlConnection1.Close() End Sub End Class


1) Create your connection string

Dim connectionString As String = "Data Source=localhost;........."

2) Connect to your Database

Dim connection As New SqlConnection(connectionString) conn.Open()

3) Create a Command and the query

Dim command As New SqlCommand("SELECT * FROM Product", connection) Dim reader As SqlDataReader = command.ExecuteReader() //Execute the Query

4) Retrieve your result. There are several ways

Dim dt As New DataTable() dt.Load(reader) 'Close the connection connection.Close()

5) Bind to your list box

myListBox.ItemSource = dt

Full code here

Using connection As New SqlConnection(connectionString) Dim command As New SqlCommand("Select * from Products", connection) command.Connection.Open() SqlDataReader reader = command.ExecuteReader() End Using

For more info

<ul><li><a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx" rel="nofollow">SQLCommand</a></li> </ul>


SqlConnection1.Open() using table As DataTable = New DataTable using command as SqlCommand = New SqlCommand("SELECT blah blah", SqlConnection1) using adapter As SqlDataAdapter = new SqlDataAdapter(command) adapter.Fill(table) end using end using for each row As DataRow in table.Rows ' add each listbox item listbox1.Items.Add(row("column name")) next end using SqlConnection1.Close()


  • Display Metadata-thumbnail of Jpeg in picturebox
  • Difference between find and closest in jquery
  • The path is not of a legal form
  • rename javascript object in order to use a php array
  • How to load an image in ASP.NET from a database as a file in a web browser?
  • Custom selection with C# DataGridView
  • Button inside Update Panel is not triggered, in asp.net
  • Operation not supported on read-only collection
  • libcurl compile errors
  • How to create a data template dependent on an XML Attribute?
  • Bind collection of objects to ListBox
  • How to access recipient on sent messages page with mailboxer
  • Need code translation from VB to C#
  • Click on button in another program - FindWindow, C#
  • Memory error in python- how to use more memory
  • Implementing “partial void” in VB
  • Differences in dis-assembled C code of GCC and Borland?
  • c# open webrowser in many tab
  • Passing 1 of 3 values to a Sub
  • How to Cache Real-time Data?
  • Very simple C++ DLL that can be called from .net
  • Java: can you cast Class into a specific interface?
  • Algorithm for a smudge tool?
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Javascript + PHP Encryption with pidCrypt
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • How does Linux kernel interrupt the application?
  • unknown Exception android
  • Checking variable from a different class in C#
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?