Getting Started with Oracle Data Provider for .NET (C# 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

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

Before starting this tutorial, you should:

1 .

Install Microsoft Visual Studio 2010

2 .

Install Oracle Database 9.2 or later or Oracle Database XE

3 .

Install Oracle 11g Oracle Data Access Components (ODAC) with Oracle Developer Tools for Visual Studio version 11.2.0.1.2 or later from OTN

Creating a Project in Visual Studio

To create a new project in Visual Studio, perform the following steps:

1 .

From your Windows Program menu, start Visual Studio. From the File menu, select New Project.

Show Screenshot for Step

 

2 .

From the Project types list, select Visual C#: 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.

Show Screenshot for Step

 

3 .

The project has been created. From View menu, select Solution Explorer to open the Solution Explorer if not already open.

Show Screenshot for Step

 

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

Show Screenshot for Step

 

2 .

Scroll down the list of Component Names and select Oracle.DataAccess. Click OK.

Show Screenshot for Step

 

Retrieving Data from the Database

To retrieve data from the database, you can add some controls to your form. Perform the following steps:

1 .

From View menu, select Toolbox. The toolbox appears. Expand Common Controls.

Show Screenshot for Step

 

2 .

Drag the Button control to the Form1 canvas.

Show Screenshot for 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.

Show Screenshot for Step

 

4 .

Double-click button1 to open the code window. Note, Form1.cs code window opens.

Show Screenshot for Step

 

5 .

Add the following C# using statements before the Public Class declaration.

using Oracle.DataAccess.Client; // ODP.NET Oracle managed provider

using Oracle.DataAccess.Types;

Show Screenshot for Step

 

6 .

Add the following C# code in between the private void button1_Click(object sender, EventArgs e)
{ and } statements.

string oradb = "Data Source=ORCL;User Id=hr;Password=hr;";

OracleConnection conn = new OracleConnection(oradb); // C#

conn.Open();

OracleCommand cmd = new OracleCommand();

cmd.Connection = conn;

cmd.CommandText = "select department_name from departments where department_id = 10"; cmd.CommandType = CommandType.Text;

OracleDataReader dr = cmd.ExecuteReader();

dr.Read();

label1.Text = dr.GetString(0);

conn.Dispose();

Show Screenshot for Step

 

7 .

Select Build > Build OraWinApp.

Show Screenshot for Step

In the output window, ensure you did not get any errors. Now you can run the form.

Show Screenshot for Step

8 .

Select Debug > Start Without Debugging.

Show Screenshot for Step

 

9 .

The form appears. Click button1.

Show Screenshot for 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.

Show Screenshot for Step

 

Incorporating Interactive Controls

Instead of running the hard coded query, you can add a textbox control to accept user input for the department id. Perform the following steps:

1 .

Select the Form1.cs [Design] tab. From the Toolbox, drag the TextBox control to Form1 canvas.

Show Screenshot for Step

The textbox is shown. You can drag it to the desired location.

Show Screenshot for Step

 

2 .

Drag a Label control on Form1 canvas.

Show Screenshot for Step

 

3 .

The Label is shown. Right-click Label2 and select Properties.

Show Screenshot for Step

 

4 .

In the Properties sheet, change the Text property to Enter Department ID:

Show Screenshot for Step

 

5 .

Select the Form1.cs tab.

Show Screenshot for 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:

OracleParameter parm = new OracleParameter();

parm.OracleDbType = OracleDbType.Decimal;

parm.Value = textBox1.Text;

Show Screenshot for Step

 

7 .

Add the cmd.Parameters statement right after the cmd.Connection statement:

cmd.Parameters.Add(parm);

Show Screenshot for Step

 

8 .

Change the code for the cmd.CommandText statement to the following:

cmd.CommandText = "select department_name from departments where department_id = :1";

Show Screenshot for Step

 

9 .

Select Build > Rebuild OraWinApp.

Show Screenshot for Step

Check in the output window for any errors reported. If no errors or warnings, proceed with the next step.

 

10 .

Select Debug > Start Without Debugging.

Show Screenshot for Step

 

11 .

Enter 20 for the Department ID and click button1.

Show Screenshot for 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.

Show Screenshot for Step

 

13 .

Enter 300 for the Department ID and click button1.

Show Screenshot for Step

 

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 know exactly why you received the error.

Show Screenshot for Step

 

Adding Error Handling

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 Form1.cs window, change the following code from:

dr.Read();
label1.Text = dr.GetString(0);

to

if (dr.Read())
{
   label1.Text = dr.GetString(0);
}
else 
{
   label1.Text = "Department ID not found";
};

Show Screenshot for Step

 

2 .

Select Build > Rebuild OraWinApp. Ensure that there are no errors reported in the output window.

Show Screenshot for Step

 

3 .

Select Debug > Start Without Debugging.

Show Screenshot for Step

 

4 .

Enter 300 again for the Department ID and click button1.

Show Screenshot for Step

 

5 .

This time, the message Department ID not found is displayed. Close Form1.

Show Screenshot for Step

 

Retrieving Multiple Columns and Rows

In this topic, you want to retrieve more than one set of information using a listbox. Perform the following steps:

 

1 .

Click the Form1.cs [Design] tab. From the Toolbox, drag the Listbox control to the Form 1 canvas.

Show Screenshot for Step

 

2 .

The new listbox appears on the form. You can position and re-size the listbox appropriately. Click Form1.cs tab.

Show Screenshot for Step

 

3 .

Delete the cmd.Parameters.Add(parm); statement.

Show Screenshot for Step

 

4 .

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";

 

Show Screenshot for Step

 

5 .

Also change the following lines of code from:

if (dr.Read())
{
label1.Text = dr.GetString(0);
}
else
{
label1.Text = "Department ID not found";
};

to

while (dr.Read())
{
listBox1.Items.Add("The " + dr.GetString(1)
+ " department is in " + dr.GetString(2));
}

Show Screenshot for Step

 

6 .

Select Build > Rebuild OraWinApp.

Show Screenshot for Step

 

7 .

Select Debug > Start Without Debugging.

Show Screenshot for Step

 

8 .

Click button1.

Show Screenshot for Step

 

9 .

The list of all the departments and their location is shown. Close Form1.

Show Screenshot for Step

 

Summary

In this tutorial, you have learned how to:

 

Hardware and Software Engineered to Work Together About Oracle |Oracle and Sun | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Your Privacy Rights