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
Background
Oracle Data Provider for .NET (ODP.NET) is Oracle's high performance ADO.NET compliant data provider that exposes a complete set of Oracle specific data access 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.
This tutorial will guide you through building a basic .NET application that retrieves data from the Oracle database. You will learn how ODP.NET interacts with standard .NET controls. Then, you will learn how to handle errors with ODP.NET. Lastly, you will modify the application to return multiple columns of data.
This tutorial uses ODP.NET, Managed Driver. It is a 100% managed code driver that does not require the traditional unmanaged Oracle Database Client.
What Do You Need?
Before starting this tutorial, you should:
Install Microsoft Visual Studio 2015 or later.
Have access to Oracle Database 12c or later.
Install Oracle Data Access Components (ODAC) 12c Release 4 or later from OTN. The ODAC download includes Oracle Developer Tools for Visual Studio and ODP.NET that will be used in this lab.
Creating a Project in Visual Studio
In this section, you will create a new Visual Studio C# application.
Open Visual Studio. Click File. Select New > Project.
Select Extensions menu under Assemblies on the side bar. Scroll down the list of extensions and select Oracle.ManagedDataAccess and click OK. This is the ODP.NET, Managed Driver assembly.
Add the following C# code in the button1_Click method. This code will connect to the HR schema, then query the DEPARTMENTS table for the department name with the ID of 10.
In this tutorial, the Data Source is the Oracle database alias,ORCL. This alias may be different from your local database server.
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();
Description of this imageNote: Since the query in the code was to show the department name for department ID of 10, the result is the Administration department.
Incorporating Interactive Controls
You can add a text box control to accept user input for the department ID, instead of running the hard coded query. Perform the following steps.
Select the Form1.cs [Design] tab. From the Toolbox, drag the TextBox control to Form1 canvas.
Modify the code for the cmd.CommandText statement to the following code.
cmd.CommandText = "select department_name from departments where department_id = :1";
Description of this imageNote: You can use a bind variable to include the value of the text box as part of the SELECT statement. Bind variables improve performance by saving the database from reparsing the same SQL statement every time the text box value changes.
Select Build > Rebuild OraWinApp.
Description of this image
Check in the output window for any errors reported. If no errors or warnings, proceed with the next step.
Error handling is needed here to instruct the user an invalid ID was entered. In this topic, you modify the code to prevent an error when an invalid Department ID is provided. 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, refer to the ODP.NET online documentation.
In Form1.cs code, 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";
};
In this section, you will retrieve more than one column of information using a listbox. The new query will be a join of the DEPARTMENTS and LOCATIONS tables. The query will return all the department IDs, the department names, and their locations. The department names and their respective location will then be displayed.
Go to Form1.cs [Design] tab. From the Toolbox, drag the ListBox control to the Form1.cs canvas.