Publish date: 2003-04-23

Work with Data Reader Object (C#, VB.Net)

Download the example archive
 /*
sample demonstrate how to work with Data Reader Object 
for extract collection of rows

using file employee.udl with Connection parameters
(plase this file to your {bin/* }  project directory)
source udl file text:
[oledb]
; Everything after this line is an OLE DB initstring

'Provider=LCPI.IBProvider;
'User ID=sysdba;
'Password=masterkey;
'Persist Security Info=True;
'Data Source=e:\database\employee.gdb;
*/

using System;
using System.Data.OleDb;  

namespace Data_Reader
{    
    class DataReaderSample
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            OleDbConnection con; 

            try
            {    
                //Create new Connection
                Console.WriteLine ("Create new connection");
                con =new OleDbConnection("file name=employee.udl");
                Console.WriteLine("Trying open connection");
                con.Open();
                
                //Create new Command
                OleDbCommand cmd; 
                Console.WriteLine("Create command");
                cmd = con.CreateCommand();

                Console.WriteLine("Start a local transaction");
                cmd.Transaction = con.BeginTransaction();   //Start a local transaction.             
                cmd.CommandText = "select * from employee"; //Set the Command Text

                //**** Data Reader ***
                OleDbDataReader myReader;                    //OleDbDataReader variable
                Console.WriteLine("Fill OleDBDataReader");
                myReader = cmd.ExecuteReader();                //fill Data Reader

                Console.WriteLine("Employers listing");
                Console.WriteLine("------------------");

                while (myReader.Read())
                {
                Console.WriteLine("Empl № " + myReader.GetInt16(0) +
                                  " : " +     myReader.GetString(1) + " " +
                                              myReader.GetString(2)); 
                }

                Console.WriteLine("------------------");
                Console.WriteLine("Close OleDbDataReader");

                myReader.Close();        //close data reader

                Console.WriteLine("Commit transaction and close connection");
                cmd.Transaction.Commit();
                cmd.Connection.Close();  //close connection

            }    
            catch(OleDbException myOLEDBException)
            { 
                //ole db exception
                //error collection sample 
                Console.WriteLine("----------------------------------------");
                for (int i = 0; i<=myOLEDBException.Errors.Count-1;i++)
                {
                    Console.WriteLine("Message " + (i + 1) + ": " + myOLEDBException.Errors[i].Message);
                    Console.WriteLine("Native: " + myOLEDBException.Errors[i].NativeError.ToString());
                    Console.WriteLine("Source: " + myOLEDBException.Errors[i].Source);
                    Console.WriteLine("SQL: " + myOLEDBException.Errors[i].SQLState);
                    Console.WriteLine("----------------------------------------");
                }
            }
            catch (Exception myException)
            {
                //other exception
                //error collection sample
                Console.WriteLine("----------------------------------------");
                Console.WriteLine(myException.Message);
            }
          
            Console.WriteLine("All Done. Press Enter to close");
            Console.ReadLine();
            
        }
    }
}
Download the example archive



Download the example archive
 'sample demonstrate how to work with Data Reader Object 
'for extract collection of rows
'
'using file employee.udl with Connection parameters
'(plase this file to your {bin} project directory)
'source udl file text:
'[oledb]
'; Everything after this line is an OLE DB initstring
'
'Provider=LCPI.IBProvider;
'User ID=sysdba;
'Password=masterkey;
'Persist Security Info=True;
'Data Source=e:\database\employee.gdb;

Imports System.Data.OleDb

Module DataReaderSample

    Sub Main()

        Dim i As Integer
        Dim con As OleDbConnection

        Try
            'Create new Connection
            Console.WriteLine("Create new connection")
            con = New OleDbConnection("file name=employee.udl")

            Console.WriteLine("Trying open connection")
            con.Open()             'Open connection

            'Create new Command
            Dim cmd As OleDbCommand
            Console.WriteLine("Create command")
            cmd = con.CreateCommand()

            Console.WriteLine("Start a local transaction")
            cmd.Transaction = con.BeginTransaction() 'Start a local transaction. 
            cmd.CommandText = "select * from employee"

            '**** Data Reader ***
            Dim myReader As OleDbDataReader
            Console.WriteLine("Fill OleDBDataReader")
            myReader = cmd.ExecuteReader() 'fill Data Reader

            Console.WriteLine("Employers listing")
            Console.WriteLine("------------------")

            Do While myReader.Read()
                Console.WriteLine("Empl № " & myReader.GetInt16(0) & _
                                  " : " & myReader.GetString(1) & " " & _
                                          myReader.GetString(2))
            Loop

            Console.WriteLine("------------------")
            Console.WriteLine("Close OleDbDataReader")

            myReader.Close()        'close data reader

            Console.WriteLine("Commit transaction and close connection")
            cmd.Transaction.Commit()
            cmd.Connection.Close()  'close connection


        Catch myOLEDBException As OleDbException 'ole db exception
            'error collection sample 
            Console.WriteLine("----------------------------------------")
            For i = 0 To myOLEDBException.Errors.Count - 1
                Console.WriteLine("Message " & i.ToString + 1 & ": " + myOLEDBException.Errors(i).Message)
                Console.WriteLine("Native: " + myOLEDBException.Errors(i).NativeError.ToString())
                Console.WriteLine("Source: " + myOLEDBException.Errors(i).Source)
                Console.WriteLine("SQL: " + myOLEDBException.Errors(i).SQLState)
                Console.WriteLine("----------------------------------------")
            Next i

        Catch myException As Exception  'other exception
            'error collection sample
            Console.WriteLine("----------------------------------------")
            Console.WriteLine(myException.Message)
            Console.WriteLine("----------------------------------------")
        End Try

        Console.WriteLine("All Done. Press Enter to close")
        Console.ReadLine()

    End Sub

End Module
Download the example archive