Getting Started With Oracle Data Provider For .NET (VB
Getting Started With Oracle Data Provider For .NET (VB
NET Version)
1 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
Purpose
This tutorial describes how you can build a .NET application that uses the Oracle Data Provider for .NET to access data in an Oracle
Database.
Time to Complete
Overview
Prerequisites
Creating a Project in Visual Studio
Adding a Reference
Retrieving Data from the Database
Incorporating Interactive Controls
Adding Error Handling
Retrieving Multiple Columns and Rows
Summary
Viewing Screenshots
Click icon to show all screenshots
Approximately 30 minutes
Overview
In addition to basic Oracle client connectivity software, .NET applications require the use of what is known as a managed data provider
(where "managed" refers to code managed by the .NET framework). The data provider is the layer between the .NET application code
and the Oracle client connectivity software.
The Oracle Data Provider for .NET (ODP.NET) is Oracle's high performance ADO.NET 2.0 compliant data provider that exposes a
complete set of Oracle specific features and tuning options including support for Real Application Clusters, XML DB, and advanced
security. It is available for free download from the Oracle Technology Network website.
When ODP.NET and any required Oracle client connectivity software is installed, application development using Visual Studio can begin.
It is a good idea to confirm client connectivity before starting development. If you can connect to Oracle using SQL*Plus on the same
machine as Visual Studio, then you know that your Oracle client-side software is properly installed and configured.
Prerequisites
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
2 . From the Project types list, select Visual Basic: Windows. Select the Template Windows Form Application. In the
Name field, enter OraWinApp. In the Location field, enter the directory where you want to save the files. (Note: if the
directory does not exist, it is created). Click OK
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
2 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
3 . The project has been created. From View menu, select Solution Explorer to open the Solution Explorer if not
already open.
Adding a Reference
Because your project needs access to an Oracle database, it is necessary to add a reference to the dll containing the data provider.
Perform the following steps:
1 . From Project menu, select Add Reference...
2 . Scroll down the list of Component Names and select Oracle.DataAccess. Click OK.
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
3 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
4 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
The control is shown on the canvas. You also need to create a Label.
3 . From the Toolbox, drag the Label control to the Form1 canvas.
4 . Double-click Button1 to open the code window. Note, Form1.vb code window opens.
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
5 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
5 . Add the following VB.NET Imports statements before the Public Class declaration.
Imports System.Data
Imports Oracle.DataAccess.Client ' ODP.NET Oracle managed provider
Imports Oracle.DataAccess.Types
6 . Add the following VB.NET code between the Private Sub and End Sub statements.
Dim oradb As String = "Data Source=orcl;User Id=hr;Password=hr;"
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandText = "select department_name from departments where department_id = 10"
cmd.CommandType = CommandType.Text
Dim dr As OracleDataReader = cmd.ExecuteReader()
dr.Read()
Label1.Text = dr.Item("department_name")
conn.Dispose()
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
6 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
In the output window, ensure you did not get any errors. Now you can run the form.
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
The result is shown in the label. Since the query in the code was to show the department name for department id of
10, the result is the Administration department. Close Form1.
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
7 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
8 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
4 . In the Properties sheet, change the Text property to Enter Department ID:
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
9 de 15
Print Preview
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
6 . You use a bind variable to include the value of the text box as part of the SELECT statement. Bind variables improve performance by preventing the
database from having to reparse the SQL statement every time the text box value changes.
Add the following code right after the conn.Open() statement:
Dim parm As New OracleParameter
parm.OracleDbType = OracleDbType.Decimal
parm.Value = TextBox1.Text
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
10 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Check in the output window for any errors reported. If no errors or warnings, proceed with the next step.
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
12 . Notice that the Label1 changes to the name of the department. This value depends on what is entered in the Department ID field.
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
11 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Viewing Screenshots
Click icon to show all screenshots
14 . This time you receive an error because there is no department with the id of 300. Click Quit. In the next topic you add some error handling so you kn
exactly why you received the error.
2 . Select Build > Rebuild OraWinApp. Ensure that there are no errors reported in the output window.
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
12 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
5 . This time, the message Department ID not found is displayed. Close Form1.
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
13 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
2 . The new listbox appears on the form. You can position and re-size the listbox appropriately. Click Form1.vb tab.
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
14 de 15
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
Print Preview
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
22/07/2015 14:13
Getting Started with Oracle Data Provider for .NET (VB.NET Version)
15 de 15
Print Preview
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/...
8 . Click button1.
Viewing Screenshots
Click icon to show all screenshots
Note: Alternatively, you can click an individual icon (or image)
associated with each step to view (or hide) the screenshot
associated with that step.
9 . The list of all the departments and their location is shown. Close Form1.
Summary
In this tutorial, you have learned how to:
Create a project and add a reference
Retrieve data from the database
Incorporate interactive controls and error handling
Retrieve Multiple Columns and Rows
22/07/2015 14:13