none
vb.net 2008, using DataGrid, How to connect with MS Access DataBase 2000?

    Question

  • vb.net 2008, using DataGrid, How to connect with MS Access DataBase 2000?, In VB6 just drag Datagrid and ADODC set the datasource can see the data in few seconds.
    Is there is easier way in vb.net 2008 to do the same action?
    Wednesday, April 16, 2008 1:48 AM

Answers

  •  StephenMas wrote:
    vb.net 2008, using DataGrid, How to connect with MS Access DataBase 2000?, In VB6 just drag Datagrid and ADODC set the datasource can see the data in few seconds.
    Is there is easier way in vb.net 2008 to do the same action?

     

    Hi StephenMas,

     

    There are two approaches to binding MS Access database table to DataGrid or DataGridView in VB.NET.

    I presume D:\myDB.mdb database file has one table named User.

     

    Approach 1:  Using Data Wizard

    1.  Data menu -> Add New Data Source

    During wizard, select D:\myDB.mdb and User table, finishing it will create a StaffDBDataSet.xsd file in Solution Explorer.

     

    2.  Data menu -> Show Data Sources  

    In Data Sources panel, drag&drop the entire User table to your Form, then UserDataGridView, UserDBDataSet, UserTableAdapter, UserBindingNavigator and UserBindingSource controls will be added automatically to Form, and UserDataGridView has been bound.

     

     

    Approach 2:  Using pure code

     

    Please check the following code sample:

    Code Snippet

    Imports System.Data.OleDb

     

    Public Class Form1

     

        ' Binding database table to DataGridView

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

            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\myDB.mdb")

            Dim cmd As OleDbCommand = New OleDbCommand("Select * FROM User", con)

            con.Open()

            Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)

            Dim myDataSet As DataSet = New DataSet()

            myDA.Fill(myDataSet, "MyTable")

            DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView

            con.Close()

            con = Nothing

        End Sub

     

         ' Retrieve table records into DataReader object

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\myDB.mdb")

                Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM User", con)

                con.Open()

                Dim sdr As OleDbDataReader = cmd.ExecuteReader()

             While sdr.Read = True

                    MessageBox.Show(sdr.Item("username"))

                End While

            Sdr.Close()

            con.Close()

            con = Nothing

     

        End Sub

     

    End Class

    Code sample: Select/Insert/Update/Delete MS Access database

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3159275&SiteID=1

     

     

    Regards,

    Martin

    Thursday, April 17, 2008 7:38 AM

All replies

  •  StephenMas wrote:
    vb.net 2008, using DataGrid, How to connect with MS Access DataBase 2000?, In VB6 just drag Datagrid and ADODC set the datasource can see the data in few seconds.
    Is there is easier way in vb.net 2008 to do the same action?

     

    Hi StephenMas,

     

    There are two approaches to binding MS Access database table to DataGrid or DataGridView in VB.NET.

    I presume D:\myDB.mdb database file has one table named User.

     

    Approach 1:  Using Data Wizard

    1.  Data menu -> Add New Data Source

    During wizard, select D:\myDB.mdb and User table, finishing it will create a StaffDBDataSet.xsd file in Solution Explorer.

     

    2.  Data menu -> Show Data Sources  

    In Data Sources panel, drag&drop the entire User table to your Form, then UserDataGridView, UserDBDataSet, UserTableAdapter, UserBindingNavigator and UserBindingSource controls will be added automatically to Form, and UserDataGridView has been bound.

     

     

    Approach 2:  Using pure code

     

    Please check the following code sample:

    Code Snippet

    Imports System.Data.OleDb

     

    Public Class Form1

     

        ' Binding database table to DataGridView

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

            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\myDB.mdb")

            Dim cmd As OleDbCommand = New OleDbCommand("Select * FROM User", con)

            con.Open()

            Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)

            Dim myDataSet As DataSet = New DataSet()

            myDA.Fill(myDataSet, "MyTable")

            DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView

            con.Close()

            con = Nothing

        End Sub

     

         ' Retrieve table records into DataReader object

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\myDB.mdb")

                Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM User", con)

                con.Open()

                Dim sdr As OleDbDataReader = cmd.ExecuteReader()

             While sdr.Read = True

                    MessageBox.Show(sdr.Item("username"))

                End While

            Sdr.Close()

            con.Close()

            con = Nothing

     

        End Sub

     

    End Class

    Code sample: Select/Insert/Update/Delete MS Access database

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3159275&SiteID=1

     

     

    Regards,

    Martin

    Thursday, April 17, 2008 7:38 AM
  • Toolbox ------> Data Tab -----> DataGrid View -----> Drag and drop to the Form --- > click the small white arrow to the upper right corner of the data grid --- > select source --- > Add Project Data Source ---- > Choose Database --- > Press New Connection --- > Press the change button beside data source --- > choose Access database Driver --- > Browse for your database , click test connection --- > continue the wizard in order to choose your tables and Voila
    • Proposed as answer by Georges N Thursday, July 29, 2010 9:25 AM
    Thursday, July 29, 2010 9:23 AM
  • Sir How Are u

    I  m  using Insert query But Its Not Working Plz Help Me Out :(

    Plz Contact me At Mzis.1991@gmail.com

    Wednesday, April 06, 2011 8:00 PM