OleDbDataReader to read data in VB.NET

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.
  • 19084

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.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.