ADO.NET Using ADO RecordSet in VB.NET
Using ADO recordset in managed code using ADO.NET data providers is pretty simple. Visual studio .NET provides you to add reference to the COM libraries. In this article I'll show you how to access data using ADO recordset and fill a ADO.NET data provider from the recordset data.
Using ADO recordset in managed code using ADO.NET data
providers is pretty simple. Visual studio .NET provides you to add reference to
the COM libraries. In this article I'll show you how to access data using ADO
recordset and fill a ADO.NET data provider from the recordset data.
This application is a Windows application. Create a
Windows application and drag a data grid control to the form from toolbox.
The first step to add a reference to the ADO library.
You can add reference to the ADO library by using Project->Add Reference menu
item and then select COM tab as you can see in Figure 1.
Figure 1. Adding reference to ADO Library.
After adding this reference, you'll see the ADODB
namespace is added to your project as you can see from Figure 2.
Figure 2. The ADODB namespace available in the
Now when you type Imports, ADODB namespace will be
available in your namespaces list as you can see from Figure 3.
Figure 3. Importing ADODB namespace.
You also need to import the System.Data and the
System.Data.OleDb since I'll use OldDb data provider to access an Access
database. Import these namespaces in your project.
Now write the following code on the form load
constructor. As you can see from the following code, I simply create a
Connection object, set its mode, and call Execute method to execute a SQL
statement. The Execute method returns a _Recordset object.
After that I simply create a dataset and data adapter
and call Fill method of data adapter by passing recordset object as a parameter.
The Fill method of data adapter fills data from a recordset to a data setr.
After that I bind data set object to a data grid.
Sub Form1_Load(ByVal sender
ByVal e As
' Create a connection string
= "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\Northwind.mdb"
Dim sql As
String = "SELECT CustomerId, CompanyName,
ContactName From Customers"
' Create a Connection object and open it
As Connection =
conn.CursorLocation = CursorLocationEnum.adUseServer
conn.Open(ConnectionString, "", "", connMode)
Dim cmdType As
Integer = CommandTypeEnum.adCmdText
Dim rs As
_Recordset = conn.Execute(sql)
' Create dataset and data adpater objects
As DataSet =
Dim da As
OleDbDataAdapter = New OleDbDataAdapter
' Call data adapter's Fill method to fill data
' Recordset to the dataset
da.Fill(ds, rs, "Customers")
' Now use dataset
DataGrid1.DataSource = ds.DefaultViewManager
The output of the program looks like Figure 4.
Figure 4. Data in a data grid accessed via ADO