ARTICLE

Using OleDbDataReader to read data

Posted by Mahesh Chand Articles | ADO.NET in VB.NET May 05, 2004
OleDbDataReader class is very useful when you need to read fast data. The OleDbDataReader class is defined in the System.Data.OleDb namespace. So before you use this class, you need to add these namespaces before using ADO.NET components.
 
Reader Level:

OleDbDataReader class is very useful when you need to read fast data. The OleDbDataReader class is defined in the System.Data.OleDb namespace. So before you use this class, you need to add these namespaces before using ADO.NET components.

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

Sample Code:

In the sample code, I've used an Access database to read the data. Same well known steps.

  • Create Connection
  • Open Connection
  • Create Command Object
  • Call Command's ExecuteReader() to return DataReader
  • Used DataReader to get the data.

Try
'construct the command object and open a connection to the Contacts table
Dim cmdString As String = "Select ContactID, FirstName, LastName from Contacts"
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\contactmanagement.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection(connString)
' Open connection
myConnection.Open()
'Create OleDbCommand object
Dim TheCommand As OleDbCommand = New OleDbCommand(cmdString, myConnection)
TheCommand.CommandType = CommandType.Text
' Create a DataReader and call Execute on the Command Object to construct it
Dim TheDataReader As OleDbDataReader = TheCommand.ExecuteReader()
While TheDataReader.Read()
System.Console.Write(TheDataReader("ContactID").ToString())
System.Console.Write(" ")
System.Console.Write(TheDataReader("FirstName").ToString())
System.Console.Write(" ")
System.Console.Write(TheDataReader("LastName").ToString())
ystem.Console.WriteLine()
End While
Catch
ae As OleDbException
MsgBox(ae.Message())
End Try

Cheers.

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.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor