Oracle by Example brandingGetting Started with ODP.NET Core

section 0Before You Begin

This 15-minute tutorial shows you how to create a basic .NET Core web application that retrieves data from the Oracle database with Oracle Data Provider for .NET (ODP.NET) Core.

Background

ODP.NET Core is an ADO.NET driver that provides fast data access from Microsoft .NET Core clients to Oracle databases. It runs on both Windows and Linux. ODP.NET consists of a single 100% managed code dynamic-link library, Oracle.ManagedDataAccess.dll, available via NuGet deployment.

In this tutorial, you will learn how to create an ASP.NET Core web application, download ODP.NET Core from nuget.org, configure ODP.NET using the OracleConfiguration class, and connect to the Oracle database.

What Do You Need?

Before starting this tutorial, you should:

  • Install Microsoft Visual Studio 2017 or later.
  • Install Oracle Database 12c or later.
  • Download the source_files.zip file.

section 1Create ASP.NET Core Web Project in Visual Studio

  1. Open Visual Studio. Click  'File'. Select New>Project.
    Creating a new project in Visual Studio
    Description of the illustration [filename]
  2. Select Visual C#>.NET Core> ASP.NET Core Web Application. Rename the Web Application as  First_Web_Application and click OK.
    Selecting .NET Core and ASP.NET Core Web Application in the wizard
    Description of the illustration [filename]
  3. Select Empty Project template and click OK.
    Select Empty in the wizard
    Description of the illustration [filename]
  4. First_Web_Application is created, you can explore the structure of the project in the Solution Explorer.
    You see the GUI with the program.cs file open and the Solution Explorer
    Description of the illustration [filename]

section 2Download ODP.NET Core from Nuget.org

In this section, you will download the ODP.NET Core NuGet package into the application.

  1. Right Click on Dependencies in the Solution Explorer and Select Manage NuGet packages.
    Solution Explorer with the right click menu. Select Manage Nuget Packages in the menu
    Description of the illustration [filename]
  2. Select Browse and search for Oracle.ManagedDataAccess.Core.
    Search for Oracle.ManagedDataAccess.Core in the NuGet Package manager
    Description of the illustration [filename]
  3. Select Oracle.ManagedDataAccess.Core and Click Install.
    Install the Oracle.ManagedDataAccess.Core pacakge into the Web application
    Description of the illustration [filename]
  4. Click OK to start the installation.
    Click OK in the preview changes wizard
    Description of the illustration [filename]
  5. Accept the license.
    Click OK for the license acceptance
    Description of the illustration [filename]
  6. You can see the installed package in the Solution Explorer.
    You will find the Oracle.ManagedDataAccess.Core in the Solution Explorer
    Description of the illustration [filename]

section 3Add ODP.NET Core Namespace and Code

In this section, we will configure the ODP.NET Core namespace and set up the data access code.

  1. Open the Startup_cs.txt file in source_files.zip. You will replace the app.Run function definition in the application's startup.cs with this code. To copy, highlight all the text in the file and type Ctrl-C.
  2. Open startup.cs in your Visual Studio ASP.NET Core project. Add a reference to Oracle.ManagedDataAccess.Client namespace near the top of the file.
    Add Oracle.ManagedDataAccess.Client into the libraries used in the application
    Description of the illustration [filename]
  3. Highlight the App.Run code segment in the Configure method in web application's startup.cs file.
    app.Run code in programstartup. cs file
    Description of the illustration [filename]
  4. Replace the app.Run code segment with the copied code by highlighting the current app.Run section, then typing Ctrl-V to paste. This code retrieves all the first names of employees in department 50 and outputs them to the web page.
    Part of the modified code in the app.Run function
    Description of the illustration [filename]
    Part of the app.Run function
    Description of the illustration [filename]

section 4Use Easy Connect to Setup Database Connection

Easy Connect provides a simple way to connect to an Oracle database without any configuration.

  1. Update the User Id and Password with the database user credentials. Populate the Data Source attribute using the Easy Connect format that corresponds to your database: <hostname or IP address>:<listener port>/<database service name>. For example,
    Hostname = localhost
    Port = 1521
    Service name = orcl
    Credentials required to access the data source added to the code
    Description of the illustration [filename]

section 5Run the Web Application

  1. You have completed all the steps needed for a basic ODP.NET web application. Run the web application by clicking on the green triangle on the menu bar.
    Execute the application by clicking on Run button
    Description of the illustration [filename]
  2. The output is all the first names of employees in department 50.
    Query result displayed on the web page
    Description of the illustration [filename]

section 6Add Configuration API Settings

In this section, you will learn about how to use ODP.NET Configuration API to configure ODP.NET programmatically.

  1. Open the config-api.txt file in source_files.zip. You will insert this code that programmatically configures ODP.NET Core. To copy, highlight all the text in the file and type Ctrl-C.
    The code to be added to the application to use Configuration API
    Description of the illustration [filename]
  2. Paste (Ctrl-V) the code at the beginning of the try block in startup.cs file. These Configuration API settings must be set before the ODP.NET connection is opened. Review the ODP.NET Configuration API settings. You see how different ODP.NET settings are modified, including adding net service names and connect descriptors.
    Location in the code where we add Configuration API specific code
    Description of the illustration [filename]

section 7Use Configuration API Connection Settings

In this section, we will modify the connection information to switch from using Easy Connect to a net service name defined in the Configuration API.

  1. Modify the connection string Data Source by replacing the Easy Connect information with orclpdb.
  2. The first line of Configuration API code added configures the orclpdb net service name. Modify the connect descriptor information on that line by modifying the HOST, PORT, and SERVICE_NAME entries that correspond to your database.
    Modified data source in the code
    Description of the illustration [filename]

section 8Run the Web Application

You have completed all the steps to configure the database connection using Configuration API.

  1. Execute the web application.
    Run the web application
    Description of the illustration [filename]
  2. You can see the database output on the web page.
    Output displayed on the webpage
    Description of the illustration [filename]

more informationWant to Learn More?