Microsoft KB Archive/308071

= How To Access an Oracle Database by Using the OleDbDataReader and Visual Basic .NET =

Article ID: 308071

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q308071



For a Microsoft Visual C# .NET version of this article, see 308448.



For a Microsoft Visual Basic 6.0 version of this article, see 176936.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data.OleDb

IN THIS TASK
SUMMARY
 * Requirements
 * Steps to Retrieve Data from an Oracle Database

REFERENCES



SUMMARY
This article demonstrates how to use the ADO.NET OleDbDataReader class to retrieve data from an Oracle database.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
 * Microsoft Data Access Components (MDAC) version 2.6 or later
 * Oracle Client tools (installed on the computer)
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * Visual Studio .NET
 * ADO.NET fundamentals and syntax
 * Oracle connectivity

back to the top

Steps to Retrieve Data from an Oracle Database
 In Oracle SQL*Plus or any Oracle Client tool that allows you to run data definition language (DDL) statements, follow these steps:   Create a table named TestTable as follows: Create Table TestTable (c1 char(5));   Insert data into TestTable as follows: Insert into TestTable c1 values('Test1'); Insert into TestTable c1 values('Test2'); Insert into TestTable c1 values('Test3');   Start Visual Studio .NET. Create a new Windows Application in Visual Basic .NET. Form1 is created by default.</li> Make sure that your project contains a reference to the System.Data namespace, and add a reference to it if it does not.</li> Drag a Button control to Form1, and change its Name property to btnTest .</li>  Use the Imports statement on the System, System.Data, and System.Data.OleDb namespaces so that you are not required to qualify declarations in those namespaces later in your code. Imports System Imports System.Data Imports System.Data.OleDb </li>  Copy and paste the following code in the code window after the &quot;Windows Form Designer generated code&quot; section: Private Sub btnTest_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnTest.Click Dim sConnectionString As String _ = &quot;Provider=MSDAORA.1;User ID=scott;password=tiger;&quot;_ &quot;Data Source=myOracleServer;Persist Security Info=False&quot; Dim mySelectQuery As String _ = &quot;SELECT * FROM TestTable where c1 LIKE ?&quot; Dim myConnection As New OleDbConnection(sConnectionString) Dim myCommand As New OleDbCommand(mySelectQuery, myConnection) 'Set the parameter value. myCommand.Parameters.Add(&quot;@p1&quot;, OleDbType.Char, 5).Value = &quot;Test%&quot; 'Open connection to Oracle database. myConnection.Open 'Populate the DataReader. Dim myReader As OleDbDataReader = myCommand.ExecuteReader Dim RecordCount as Integer Try While myReader.Read RecordCount = RecordCount + 1 MessageBox.Show(myReader.GetString(0).ToString) End While If RecordCount = 0 then MessageBox.Show(&quot;No data returned&quot;) Else MessageBox.Show(&quot;Number of records returned: &quot; & RecordCount) End If   Catch ex As Exception MessageBox.Show(ex.ToString) Finally 'Close all objects. myReader.Close myConnection.Close End Try End Sub </li> Save your project.</li> On the Debug menu, click Start to run your project.</li> Click the button to display the data.</li></ol>

back to the top

<div class="references_section">