Get data from database

I am having a hard time understanding how to access information from a database using VB.  What I need is somone to give me examples and explanations to do the following so I can use it as a base to learn more indepth stuff.  What I need is somone to show or explain how to do the following:

I have a database (microsoft access db .mdb) named mydb.mdb, it has the following fields (id,name,number)... all I want to do is select and display all the records where the ID = 5.
LVL 4
shawnlehnerAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi  shawnlehner;

Here is some code I just put together you need to change 3 lines they are marked where changes need to be made.

 Imports System.Data.OleDb

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private myDataAdapter As OleDbDataAdapter
    Private myDataSet As DataSet
    Private myDataTable As DataTable

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lbTableData As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lbTableData = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'lbTableData
        '
        Me.lbTableData.Location = New System.Drawing.Point(32, 16)
        Me.lbTableData.Name = "lbTableData"
        Me.lbTableData.Size = New System.Drawing.Size(200, 134)
        Me.lbTableData.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.lbTableData)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        ' Change the "data source=" to the path with filename of where your
        ' database is
        Dim connectionString As String = _
            "provider=Microsoft.Jet.OLEDB.4.0; " & _
            "data source=C:\temp\mydb.mdb"

        ' Change the word TableName with the actual table name
        Dim commandString As String = _
        "Select * from TableName Where id = 5"

        ' This creates the interface between the data set and the
        ' database it is connected to
        myDataAdapter = _
            New OleDbDataAdapter( _
                commandString, connectionString)

        ' Create the data set that holds the local copy of the database
        ' you have selected
        myDataSet = New DataSet
        ' Fill the data set with the selected data
        ' Change TableName with the actual table name
        myDataAdapter.Fill(myDataSet, "TableName")

        ' A DataTable represents one table of in-memory relational data
        ' the one that is in the dataset
        myDataTable = myDataSet.Tables(0)
        lbTabledata.Items.Clear()
        Dim dataRow As DataRow
        ' Read each element of the myDataTable in to a list box
        For Each dataRow In myDataTable.Rows
            lbTableData.Items.Add((dataRow("id").ToString() & _
                "    " & dataRow("name").ToString() & dataRow("number").ToString()))
        Next dataRow

    End Sub
End Class
0
 
davidrichardsonCommented:
use the wizard to create a form and display the data, (add new item, dataform wizard)
once complete edit the data adapter(right click, configur data adapter)
when you get to the query click "query Builder" , in the id column criteria add   =5
ok to everything and run

0
 
grayeCommented:
I can recommend an article on beginning ADO.Net at http://home.hot.rr.com/graye/Articles/BeginningADO.htm

Lots of explainations, comments, downloadable samples, etc
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.