Solved

Need help using VB.NET to retrieve ACCESS data.

Posted on 2004-10-21
223 Views
Last Modified: 2010-04-23
I am a complete newbie to VB.NET programming. All I am trying to do is retrieve all the data from an ACCESS database and then loop through it to store it in an array.

Maybe I dont need to store it in an array. The concepts of datasets are strange to me. How do I access specific records in the datasets. I have a book on the subject, but it is only using bound controls (the datagrid). I don't want to use the bound controls at all.

I have set up my database connection(OleDbConnection1) and data adapter(OleDbAdapter1). I also have a dataset ("Blinds_Dataset").  The table is very simple. an ID (key) column, and two data columns.

This is so frustrating compared to the old way of SQL into recordsets or arrays.  ((its been 10 years since I wrote even one line of code =). )

Thanks

M
0
Question by:dunnieboy
    1 Comment
     
    LVL 28

    Accepted Solution

    by:
    Imports System.Data.OleDb

    Public Class Form1
       Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "
    #End Region
       Dim fsConn As String = _
      "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=C:\TestDB\TestDB.mdb;"

       Dim ds As DataSet
       Dim da As OleDbDataAdapter
       Dim fsSQL As String = "SELECT * FROM Test"
       Dim fsSource As String = "Test"

       Private Sub CreateDS()
           ds = New DataSet()
           'Connect to database and specify sSQL
           da = New OleDbDataAdapter(fsSQL, fsConn)

           Try
               'using DataAdapter enter records from table to DataSet
               da.Fill(ds, fsSource) 'fsSource is a table
           Catch objExc As System.Exception
               'do something
               Exit Sub
           End Try
       End Sub

       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           Me.CreateDS()
           dg.DataSource = ds.Tables("Test")
            dg.DataMember = "Test"
       End Sub

    End Class



    now you have all the records in the datatable  Test


    you can read these records like that

    Dim dr As DataRow

    For Each dr In Ds.Tables("Test").Rows
       Listbox1.Items.Add(dr.Item("Field1").ToString)
    Next
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now