Get data from database

Posted on 2005-05-12
Medium Priority
Last Modified: 2010-04-23
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.
Question by:shawnlehner

Expert Comment

ID: 13991361
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

LVL 41

Expert Comment

ID: 13991661
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
LVL 64

Accepted Solution

Fernando Soto earned 1000 total points
ID: 13991720
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()

        'This call is required by the Windows Form Designer.

        '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
            End If
        End If
    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.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
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "Form1"

    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)
        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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question