ARTICLE

Using ADO RecordSet in ADO.NET

Posted by Mahesh Chand Articles | ADO.NET in VB.NET May 10, 2003
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.
 
Reader Level:

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 project.

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.

Imports System.Data
Imports System.Data.OleDb
Imports ADODB

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.

Source Code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Create a connection string
Dim ConnectionString As 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
Dim conn As Connection = New Connection
Dim connMode As Integer = ConnectModeEnum.adModeUnknown
conn.CursorLocation = CursorLocationEnum.adUseServer
conn.Open(ConnectionString, "", "", connMode)
Dim recAffected As Object
Dim cmdType As Integer = CommandTypeEnum.adCmdText
Dim rs As _Recordset = conn.Execute(sql)
' Create dataset and data adpater objects
Dim ds As DataSet = New DataSet("Recordset")
Dim da As OleDbDataAdapter = New OleDbDataAdapter
' Call data adapter's Fill method to fill data from ADO
' Recordset to the dataset
da.Fill(ds, rs, "Customers")
' Now use dataset
DataGrid1.DataSource = ds.DefaultViewManager
End Sub

The output of the program looks like Figure 4.

Figure 4. Data in a data grid accessed via ADO recordset.

share this article :
post comment
 

I can now access the data using adodb and an ado recordset. The reason that I wanted to use the ado recordset, is because inserting and updating is so much easier with the recordset and MS Access databases. However, I keep getting an error that the recordset is not updatable and have done everything that I can find on Google, to solve the problem, but it won't go away. Any suggestions?

Posted by Daniel Heath Mar 29, 2009

You need to modify this line..

DataGrid1.DataSource = ds.DefaultViewManager

Change to..

DataGrid1.DataSource = ds.Tables(0)

Hope this would help..

 

Posted by stealth Jan 14, 2008

Hi Mahesh, I hope you can help me out with this. I have a lot of trouble finding out how to export data from a dataset to a MS Excel spreadsheet using a stylesheet. I use Dataset.WriteXML(filename) to export the data but I don't know how to write the stylesheet file that can match/transform the XML file that Excel recognises. I have tried using RecordSet/StyleSheet to export but this method turns all the unicode (chinese characters) to rubbish. I am stuck. Can you give me some pointers??

Posted by Denis Toby Oct 24, 2007

You will have to debug the code to see if data is actually coming from the database.

Posted by Mahesh Chand Feb 02, 2007

Hi Mahesh, I am Jats. I hav used the code in the article but there is no output in the display, no error even. Waiting for any suggestions. Dought: Can this code be used on Internet. Can i access the database(in server) from a client machine in LAN. Jats

Posted by jats Feb 02, 2007
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.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor