Net-informations.com
SiteMap  | About    

Dataadapter with DataGridView - OLEDB

OleDbDataAdapter provides the communication between the Dataset and the OLEDB Data Source with the help of OleDbConnection Object . The OleDbConnection Object has no information about the data it retrieves . Similarly a Dataset has no knowledge of the Data Source where the data coming from. So the OleDbDataAdapter manage the communication between these two Objects. The TableMapping Collection help the OleDbDataAdapter to do this task.

The InsertCommand, the UpdateCommand, and the DeleteCommand properties of the OleDbDataAdapter object update the database with the data modifications that are run on a DataSet object. The following source code demonstrate how to update a Dataset through OleDbDataAdapter using a DataGridView. For running this source code , open a new VB.NET project and drag two buttons and a DataGridView in the default form1 and copy and paste the following Source Code.




Imports System.Data.OleDb
Public Class Form1
    Dim connetionString As String
    Dim connection As OleDbConnection
    Dim oledbAdapter As OleDbDataAdapter
    Dim oledbCmdBuilder As OleDbCommandBuilder
    Dim ds As New DataSet
    Dim changes As DataSet
    Dim i As Integer
    Dim sql As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
        connection = New OleDbConnection(connetionString)
        Sql = "select * from tblUsers"
        Try
            connection.Open()
            oledbAdapter = New OleDbDataAdapter(Sql, connection)
            oledbAdapter.Fill(ds)
            DataGridView1.Data Source= ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            oledbCmdBuilder = New OleDbCommandBuilder(oledbAdapter)
            changes = ds.GetChanges()
            If changes IsNot Nothing Then
                oledbAdapter.Update(ds.Tables(0))
            End If
            ds.AcceptChanges()
            MsgBox("Save changes")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class




net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.