ARTICLE

DataAdapter in ADO.NET

Posted by Munesh Sharma Articles | ADO.NET in VB.NET April 01, 2011
In this article we will discuss about the functionality of DataAdapter.
Reader Level:

DataAdapter:

Data adapters are an integral part of ADO.NET managed providers, which are the set of objects used to communicate between a data source and a dataset. (In addition to adapters, managed providers include connection objects, data reader objects, and command objects.)Adapters are used to exchange data between a data source and a dataset. In many applications, this means reading data from a database into a dataset, and then writing changed data from the dataset back to the database. However, a data adapter can move data between any source and a dataset.

DataAdapter Model:

DataAdapter contain only four commands, that are:

     DataAdapter-Model.gif

Note: Generally, adapters are configurable to allow you to specify what data to move into and out of the dataset. Often this takes the form of references to SQL statements or stored procedures that are invoked to read or write to a database.

OleDbDataAdapter:

The OleDbDataAdapter serves as a bridge between a DataSet and data source for retrieving and saving data. When the OleDbDataAdapter fills a DataSet, it will create the necessary tables and columns for the returned data if they do not already exist.

Syntax of OleDbAdapter in VB.NET:

Public Function SelectOleDb(ByVal dataSet As DataSet,
                                      ByVal
connection As String, ByVal query As String) As DataSet
        Dim conn As New OleDbConnection(connection)
        Dim adapter As New OleDbDataAdapter()
        adapter.SelectCommand = New OleDbCommand(query, conn)
        adapter.Fill(dataSet)
        Return dataSet
    End Function

Steps in  OleDbAdapter in VB.NET:

  • In First Step to  Design the window form:

    Design-window-form.gif

  • In second step to Design the Data Base:

    Data-Base-Design.gif

Module1 Coding:

Imports System.Data.OleDb
Module
Module1
    Public constring As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\my_db.accdb")

End
Module

Form Coding:

Imports System.Data.OleDb 
Public
Class Form1
    Dim cmd As OleDbCommand
    Dim reader As OleDbDataReader
    Dim SqlString As String
    Dim numAffected As Integer
    Dim S As String
    Dim Ds As DataSet
    Dim Da As OleDbDataAdapter 
    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click 
    End Sub
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged 
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) 
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            constring.Open()
            SqlString = "insert into info values("
            SqlString += """" & TextBox1.Text & """"
            SqlString += ","
            SqlString += """" & TextBox2.Text & """"
            SqlString += ","
            SqlString += """" & TextBox3.Text & """"
            SqlString += ")"
 
            cmd = New OleDbCommand(SqlString, constring)
            cmd.ExecuteNonQuery()
            MessageBox.Show("a record has been successfully added")  
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            Ds = New DataSet
            SqlString = "select * from info"
            Da = New OleDbDataAdapter(SqlString, constring)
            Da.Fill(Ds, "info")
            DataGridView1.DataSource = Ds.Tables(0)                     
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub DataGridView1_CellContentClick_1(ByVal sender As System.Object, ByVal e AsSystem.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick 
    End Sub

End
Class 

Output:

 

        Output-of-data-adapter.gif



      out-put-data.gif

Additional Resource: MSDN

 

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor