Getting Started with Oracle Data Provider for .NET (VB Version)
Building .NET Applications Using Oracle Developer Tools For Visual Studio .NET
Getting Started with Oracle Data Provider for .NET (VB Version)
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.
Approximately 30 minutes
This tutorial covers the following topics:
Place the cursor over this icon to load and view all the screenshots for this tutorial.
(Caution: This action loads all screenshots simultaneously, so response time may be slow depending on your Internet connection.)
Note: Alternatively, you can place the cursor over an individual icon in the following steps to load and view only the screenshot associated with that step. You can hide an individual screenshot by clicking it.
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's a good idea to confirm client connectivity before starting development. If you can connect to Oracle using SQL*Plus on the same machine as VS.NET, then you know that your Oracle client-side software is properly installed and configured.
If you are new to Oracle, see the section "Connecting to the Oracle Database" in the Oracle Data Provider for .NET Developer's Guide 10g for background information regarding ODP.NET specifically, or to the Oracle Database Administrator's Guide 10g for information about managing the Oracle Database generally. You can also consult the "Connect to an Oracle Database Using ODP.NET" sample code "how-to" document.
Back to Topic List
Now you can create a new project. Perform the following steps:
| 1. |
In the Recent Projects area next to Create: select Project.
|
| 2. |
Accept the default Project Type Visual Basic: Windows, select the Template Windows Application , enter OraWinApp for the Name and enter the directory where you want the files stored in the location field (Note: if the directory does not exist, it is created). Then click OK.
|
| 3. |
The project has been created. Open the Solution Explorer view from the View menu if not already open.
|
Back to Topic List
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. |
Select Project > Add Reference...
|
| 2. |
Scroll down the list of References and select Oracle.DataAccess and click OK.
|
Back to Topic List
To retrieve data from the database, you can add some controls to your form. Perform the following steps:
| 1. |
Click View from the menu bar and select Toolbox from the drop-down menu or put your cursor on the Toolbox to view the menu and expand Common Controls.
|
| 2. |
Select the Button control and drag it to the form canvas.
|
| 3. |
The control is shown on the canvas. You also need to create a Label.
|
| 4. |
Open the Toolbox again and drag and drop the Label control to the canvas.
|
| 4. |
Double-click the button to open the code window.
|
| 5. |
Add the 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()

|
| 7. |
Select Build > Build Solution.
|
| 8. |
The project was built successfully. Now you can run the form.
|
| 9. |
Select Debug > Start Without Debugging.
|
| 10. |
The form appears. Click Button1.
|
| 11. |
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.
|
Back to Topic List
Instead of running the hard coded query, a textbox control can be added to accept user input for the department id. Perform the following steps:
| 1. |
Select the Form1.vb [Design] tab. Select the Toolbox and click and drag the TextBox control to your canvas.
|
| 2. |
The textbox is shown. You can drag it to the desired location.
|
| 3. |
Select the Toolbox again and drag and drop a Label control on your canvas.
|
| 4. |
The Label is shown. Right-click Label2 and select Properties.
|
| 5. |
Change the Text to Enter Department ID:. Close the Properties window.
|
| 6. |
Select the Form1.vb code tab.
|
| 7. |
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
|
| 8. |
Add the cmd.Parameters statement right after the cmd.Connection statement:
cmd.Parameters.Add(parm)
|
| 9. |
Change the code for the cmd.CommandText statement to the following:
cmd.CommandText = "select department_name from departments where department_id = :1"
|
| 10. |
Select Build > Rebuild Solution.
|
| 11. |
Select Debug > Start Without Debugging.
|
| 12. |
Enter 20 for the Department ID and click Button1.
|
| 13. |
Notice that the Label1 changes to the name of the department. This value depends on what is entered in the Department ID field.
|
| 14. |
Enter 300 for the Department ID and click Button1.
|
| 15. |
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 know exactly why you received the error.
|
Back to Topic List
Error handling is needed to instruct the user why something doesn't work properly. In this topic, you modify your code to prevent an error when an invalid Department ID is provided. In this tutorial, you add some very simple error handling code. For more robust error handling, Structured Exception Handling should be used. For more information on Structured Exception Handling with ODP.NET please refer to the ODP.NET online documentation. Perform the following steps:
| 1. |
In the Form1.vb code, change the following code from:
dr.Read()
label1.Text = dr.Item("department_name");
to
If dr.Read() Then Label1.Text = dr.Item("department_name") Else Label1.Text = "Department ID not found" End If
|
| 2. |
Select Build > Rebuild Solution.
|
| 3. |
Select Debug > Start Without Debug.
|
| 4. |
Enter 300 again for the Department ID and click Button1.
|
| 5. |
This time, the message Department ID is not found is displayed. Close Form1.
|
Back to Topic List
In this topic, you want to retrieve more than one set of information using a listbox. Perform the following steps:
| 1. |
Select the Form1.vb [Design] tab. Select the Toolbox and drag and drop the Listbox control to the canvas.
|
| 2. |
The new listbox appears on the form. Click the Form1.vb code tab.
|
| 3. |
Delete the cmd.Parameters.Add (parm) statement.
|
| 3. |
Change the cmd.CommandText code from:
cmd.CommandText = "select department_name from departments where department_id = :1"
to
cmd.CommandText = "select department_id, department_name, city" _
+ " from departments d, locations l" _ + " where d.location_id = l.location_id"
|
| 4. |
Also change the following lines of code from:
If dr.Read() Then
Label1.Text = dr.Item("department_name")
Else
Label1.Text = "Department ID not found"
End If
to
While dr.Read() ListBox1.Items.Add("The " + dr.Item(1) _ + " department is in " + dr.Item("city")) End While
|
| 5. |
Select Build > Rebuild Solution.
|
| 6. |
Select Debug > Start Without Debugging.
|
| 7. |
Click Button1.
|
| 8. |
The list of departments is shown.
|
Back to Topic List
In this tutorial, you learned how to:
 |
Add a connection to the database |
 |
Create a project and add a reference |
 |
Retrieve data from the database |
 |
Incorporate interactive controls and error handling |
 |
Retrieve Multiple Columns and Rows |
Back to Topic List
Move
your mouse over this icon to hide all screenshots.
|