Results 1 to 6 of 6

Thread: MS Access VB.NET

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    4

    MS Access VB.NET

    Hi!
    I have and Microsoft Access database that I want to access with VB.NET.
    What is the best way? I know how to use the Data Grid but I'm not sure what are the binding sources and Dataset...can I use them to do a query in the MS Access?
    Right now I'm using something similar to:
    Code:
        Dim conexao As New OleDb.OleDbConnection
            Dim comando As New OleDb.OleDbCommand
            Dim adaptador As New OleDb.OleDbDataAdapter
            Dim dados As New DataTable
            conexao = New OleDb.OleDbConnection
            conexao.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\DB.accdb'"
            Try
                conexao.Open()
                Try
                    comando.Connection = conexao
                    adaptador.SelectCommand = comando
    
                    Dim query = "SELECT Name, Diameter FROM Construction WHERE  (Name LIKE 'PF')"
                    Dim dc = New OleDb.OleDbCommand(query, conexao)
                    Dim rows As OleDb.OleDbDataReader
                    rows = dc.ExecuteReader
    
    ???? how can I get the rows values? I'm not being able to to that...
    
                Catch erro As Exception
                    MessageBox.Show("Error")
                End Try
            Catch erro As Exception
                MessageBox.Show("Error")
            Finally
                conexao.Dispose()
            End Try

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: MS Access VB.NET

    A data reader is a forward-only data reader used to read the data at high speed. However, it can't be used to bind to a DataGridView. So if you're wanting to bind to a datagridview, you'll need something like this:

    vb Code:
    1. Dim DT as DataTable
    2.  
    3. adaptador.Fill(DT)
    4.  
    5. DataGridView1.DataSource = DT
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    4

    Re: MS Access VB.NET

    Quote Originally Posted by dolot View Post
    A data reader is a forward-only data reader used to read the data at high speed. However, it can't be used to bind to a DataGridView. So if you're wanting to bind to a datagridview, you'll need something like this:

    vb Code:
    1. Dim DT as DataTable
    2.  
    3. adaptador.Fill(DT)
    4.  
    5. DataGridView1.DataSource = DT
    hi! thanks for you help.
    I don't want to use a data grid. I want to pass an value from one column to a variable.. can i use data reader? if so how?

    thanks a lot!

  4. #4
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: MS Access VB.NET

    Maybe something like this:
    vb Code:
    1. Using reader As OleDb.OleDbDataReader = dc.ExecuteReader
    2.             While reader.Read
    3.                 'do something...
    4.                 Dim I As Integer
    5.                 I = reader!Column1
    6.             End While
    7.         End Using
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: MS Access VB.NET

    or not... that's the old ADO way....
    ad

    o.
    net doesn't support the ! notation.

    For this you'll use the rows.GetString(0) to get the first column. you wouls use .getXXXX() where XXXX is the type the column is, string, decimal, date, etc... and then pass it the ordinal position of the column you want.


    this is all in the documentation https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Addicted Member
    Join Date
    Mar 2007
    Posts
    183

    Re: MS Access VB.NET

    FYI, Visual Studio can add database connections automatically. Google "add data source visual studio (visual studio version here)".
    Do not read this sentence.
    You read that last one, didn't you?
    Darn. You now read the previous two.

    Check out my pins on Pinterest!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width