ADO.NET DataAdapter Example in VB.NET
In this article I will explain about DataAdapter Example in ADO.NET
Now
you'll create your first sample using data adapters. In this sample example,
I'll show you how to create data adapters using Sql and OleDb data providers and
fill data from data from data adapter to a DataGrid control.
First, create a Windows application using Visual Basic projects and add two
buttons and a DataGrid control to the form by dragging the controls form the
toolbox to the form. Second, set both button's Name property; use
OleDbDataAdapter and SqlDataAdapter. Next set the properties to OleDbData
Adapter and SQL DataAdapter. After setting these properties, the form will look
like figure 5-40. As you can see, there are two buttons, OleDbDataAdapter and
SQL DataAdapter.
Now add button-click event handlers for both the OleDbDataAdapter and SQL
DataAdapter buttons. You can add a button-click event handler either by
double-clicking on the button or by using the Events tab of the properties
window of a button control. On the OleDb DataAdapter button-click event handler,
you'll write code to read data from an OleDb data source and fill data to the
data grid. On the SQL DataAdapter button-click event handler, you'll write code
to read data from a SQL Server datasource and fill data to the data grid.

Figure 5-40. Creating a Windows Forms application
and adding controls to the form
Listing 5-44 shows the source code for the OleDb DataAdapter button click, and
Listing 5-44 shows the source code for the SQL DataAdapter button click, and
Listing 5-45 shows the source code for the SQL DataAdatper button click. As you
can see, you follow the same steps as before. Open a connection, create a data
adapter object with a SELECT string, create a dataset object, call data
adapter's FILL method to fill the dataset, and bind the dataset to the
DataGrid.DataSource property as DataSet.DefaultViewManager, which represents the
default view of a DataSet object.
Listing 5-44. Displaying the Order table data in
a Data Grid
Imports OleDbDataAdapter
Private Sub OleDbDataAdapter_Click(ByVal sender As Object, ByVal Args As System.Event)
'Create
a connection object
Dim ConnectionString As String = "provider=Microsoft.Jet.OLEDB.4.0;" + "Data
Source= C:/northwind.mdb"
Dim SQL As String = "SELECT
* FROM Orders"
Dim conn As OleDbConnection
= New OleDbConnection(ConnectionString)
'
open the connection
conn.Open()
'
Create an OleDbDataAdapter object
Dim adapter As OleDbDataAdapter
= New OleDbDataAdapter()
adapter.SelectCommand = New OleDbCommand(SQL,
conn)
'
Create Data Set object
Dim ds As DataSet
= New DataSet("orders")
'
Call DataAdapter's Fill method to fill data from the
'
DataAdapter to the DataSet
adapter.Fill(ds)
'
Bind dataset to a DataGrid control
dataGrid1.DataSource = ds.DefaultViewManager
End Sub
The output of Listing 5-44 looks like figure 5-41.

Figure 5-41. Filling data from an Access
database to a DataGrid control using OleDbDataAdapter
The output of listing looks like figure 5-42.

Figure 5-42. Filling data from a SQL server
database to a DataGrid control using SqlDataAdapter
Listing 5-45. Displaying the Customers tables
data in a DataGrid
Imports SqlDataAdapter
Private Sub SqlDataAdapter_Click(ByVal sender As Object, ByVal e As System.EventArgs)
String ConnectionString
= "Integrated
Security = SSPI;" +
"Initial catalog = Northwind;" + "
Data Source =MAIN-SERVER; "
Dim SQL As String = "SELECT
CustomerID, CompanyName FROM Customers"
Dim conn As SqlConnection
= New SqlConnection(ConnectionString)
'
open the connection
conn.Open()
'Create
a SqlDataAdapter object
Dim adapter As SqlDataAdapter
= New SqlDataAdapter(SQL,
conn)
'
Call DataAdapter's Fill method to fill data from the
'
Data Adapter to the DataSet
Dim ds As DataSet
= New DataSet("Customers")
adapter.Fill(ds)
'
Bind data set to a DataGrid control
dataGrid1.DataSource = ds.DefaultViewManager
End Sub
Conclusion
Hope this article would have helped you in understanding DataAdapter
Example in ADO.NET. See
my other articles on the website on ADO.NET.