These tutorials show you how to connect .NET applications to Oracle Autonomous Database (ADB) using Oracle Data Provider for .NET Core via the command line, with Visual Studio Code, and with Visual Studio. Also learn how to connect .NET Framework apps to ADB using Visual Studio and Oracle Data Provider for .NET. Visit Developing .NET Applications for Oracle Database (On-Premises) for a tutorial using an on-premises database.
This tutorial shows you how use the command line to connect .NET Core applications to Oracle Autonomous Database (ADB) using Oracle Data Provider for .NET (ODP.NET) Core. Follow the steps below:
Click on the links below to walk through these steps if you have not yet provisioned an ADB instance. Remember the database name and password. You will use them to connect .NET to ADB.
You may select the Always Free option to use the Always Free Oracle ADB. Choose the Shared Infrastructure deployment type, which will have the Sales History schema that the ODP.NET sample code uses.
Below are the instructions to download the client credentials from the Oracle Cloud Console. Alternatively, obtain the credentials from your administrator.
dotnet new console
dotnet add package Oracle.ManagedDataAccess.Core
using System;
using Oracle.ManagedDataAccess.Client;
namespace ODP.NET_Core_Autonomous
{
class Program
{
static void Main()
{
//Enter your ADB's user id, password, and net service name
string conString = "User Id=<ADMIN>;Password=<MYPASSWORD>;Data Source=<mydb_high>;Connection Timeout=30;";
//Enter directory where you unzipped your cloud credentials
OracleConfiguration.TnsAdmin = @"<MYDIRECTORY>";
OracleConfiguration.WalletLocation = OracleConfiguration.TnsAdmin;
using (OracleConnection con = new OracleConnection(conString))
{
using (OracleCommand cmd = con.CreateCommand())
{
try
{
con.Open();
Console.WriteLine("Successfully connected to Oracle Autonomous Database");
Console.WriteLine();
cmd.CommandText = "select CUST_FIRST_NAME, CUST_LAST_NAME, CUST_CITY, CUST_CREDIT_LIMIT " +
"from SH.CUSTOMERS order by CUST_ID fetch first 20 rows only";
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1) + " in " +
reader.GetString(2) + " has " + reader.GetInt16(3) + " in credit." );
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
}
dotnet run
This sample application uses the Sales History (SH) sample schema. SH is a data set suited for online transaction processing operations. The Star Schema Benchmark (SSB) sample schema is available for data warehousing operations. Both schemas are available with your shared ADB instance and do not count towards your storage. You can use any ADB user account to access these schemas.
Note: If you connect to ADB from behind a firewall, you will likely encounter a connection timeout error. Set your ODP.NET network configuration to use an HTTP proxy to connect successfully to ADB. Learn how to do this in this ADB documentation section. Scroll down to the "Connections with an HTTP Proxy" section on the doc page.
This tutorial shows you how to connect .NET Core applications to Oracle Autonomous Database (ADB) using Oracle Data Provider for .NET (ODP.NET) Core, Visual Studio Code, and the Oracle Developer Tools for VS Code extension. Follow the steps below:
Click on the links below to walk through these steps if you have not yet provisioned an ADB instance. Remember the database name and password. You will use them to connect .NET to ADB.
You may select the Always Free option to use the Always Free Oracle ADB. Choose the Shared Infrastructure deployment type, which will have the Sales History schema that the ODP.NET sample code uses.
Below are the instructions to download the client credentials from the Oracle Cloud Console. Alternatively, obtain the credentials from your administrator.
select CUST_FIRST_NAME, CUST_LAST_NAME, CUST_CITY, CUST_CREDIT_LIMIT from sh.customers order by CUST_ID fetch first 20 rows only
dotnet new console
dotnet add package Oracle.ManagedDataAccess.Core
using System;
using Oracle.ManagedDataAccess.Client;
namespace ODP.NET_Core_Autonomous
{
class Program
{
static void Main()
{
//Enter your ADB's user id, password, and net service name
string conString = "User Id=<ADMIN>;Password=<MYPASSWORD>;Data Source=<mydb_high>;Connection Timeout=30;";
//Enter directory where you unzipped your cloud credentials
OracleConfiguration.TnsAdmin = @"<MYDIRECTORY>";
OracleConfiguration.WalletLocation = OracleConfiguration.TnsAdmin;
using (OracleConnection con = new OracleConnection(conString))
{
using (OracleCommand cmd = con.CreateCommand())
{
try
{
con.Open();
Console.WriteLine("Successfully connected to Oracle Autonomous Database");
Console.WriteLine();
cmd.CommandText = "select CUST_FIRST_NAME, CUST_LAST_NAME, CUST_CITY, CUST_CREDIT_LIMIT " +
"from SH.CUSTOMERS order by CUST_ID fetch first 20 rows only";
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1) + " in " +
reader.GetString(2) + " has " + reader.GetInt16(3) + " in credit." );
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
}
dotnet run
This sample application uses the Sales History (SH) sample schema. SH is a data set suited for online transaction processing operations. The Star Schema Benchmark (SSB) sample schema is available for data warehousing operations. Both schemas are available with your shared ADB instance and do not count towards your storage. You can use any ADB user account to access these schemas.
Note: If you connect to ADB from behind a firewall, you will likely encounter a connection timeout error. Set your ODP.NET network configuration to use an HTTP proxy to connect successfully to ADB. Learn how to do this in this ADB documentation section. Scroll down to the "Connections with an HTTP Proxy" section on the doc page.
This tutorial shows you how to connect .NET Core applications to Oracle Autonomous Database (ADB) using Oracle Data Provider for .NET (ODP.NET) Core, Visual Studio 2019, and the Oracle Developer Tools for Visual Studio extension. Follow the steps below.
(Note: If you have already completed the tutorial for .NET Framework with Visual Studio, you can skip to the last step of this tutorial. The other steps are identical.)
If you already have an ADB instance that you would like to use, you can skip to step 4: Connect to Oracle Autonomous Database.
In this step we will test a SQL statement that we will include in our application later. The sample application uses the Sales History (SH) sample schema. SH is a data set suited for online transaction processing operations. This schema is available with your shared ADB instance and do not count towards your storage. You can use any ADB user account to access these schemas.
select CUST_FIRST_NAME, CUST_LAST_NAME, CUST_CITY, CUST_CREDIT_LIMIT
from sh.customers order by CUST_ID fetch first 20 rows only
In this step you will build a .NET Core application that can run on Windows, Linux, and macOS.
using System;
using Oracle.ManagedDataAccess.Client;
namespace ODP.NET_Core_Autonomous
{
class Program
{
static void Main()
{
//Enter your ADB's user id, password, and net service name
string conString = "User Id=<ADMIN>;Password=<MYPASSWORD>;Data Source=<mydb_high>;Connection Timeout=30;";
//Enter directory where you unzipped your cloud credentials
OracleConfiguration.TnsAdmin = @"<MYDIRECTORY>";
OracleConfiguration.WalletLocation = OracleConfiguration.TnsAdmin;
using (OracleConnection con = new OracleConnection(conString))
{
using (OracleCommand cmd = con.CreateCommand())
{
try
{
con.Open();
Console.WriteLine("Successfully connected to Oracle Autonomous Database");
Console.WriteLine();
cmd.CommandText = "select CUST_FIRST_NAME, CUST_LAST_NAME, CUST_CITY, CUST_CREDIT_LIMIT " +
"from SH.CUSTOMERS order by CUST_ID fetch first 20 rows only";
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1) + " in " +
reader.GetString(2) + " has " + reader.GetInt16(3) + " in credit." );
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
}
Note: If you connect to ADB from behind a firewall, you will likely encounter a connection timeout error. Set your ODP.NET network configuration to use an HTTP proxy to connect successfully to ADB. Learn how to do this in this ADB documentation section. Scroll down to the "Connections with an HTTP Proxy" section on the doc page.
This tutorial shows you how to connect .NET Framework applications to Oracle Autonomous Database (ADB) using Oracle Data Provider for .NET (ODP.NET), Visual Studio 2019, and the Oracle Developer Tools for Visual Studio extension. Follow the steps below.
(Note: If you have already completed the tutorial for .NET Core with Visual Studio, you can skip to the last step of this tutorial. The other steps are identical.)
If you already have an ADB instance that you would like to use, you can skip to step 4: Connect to Oracle Autonomous Database.
In this step we will test a SQL statement that we will include in our application later. The sample application uses the Sales History (SH) sample schema. SH is a data set suited for online transaction processing operations. This schema is available with your shared ADB instance and do not count towards your storage. You can use any ADB user account to access these schemas.
select CUST_FIRST_NAME, CUST_LAST_NAME, CUST_CITY, CUST_CREDIT_LIMIT
from sh.customers order by CUST_ID fetch first 20 rows only
In this step you will build a .NET Framework application.
using System;
using Oracle.ManagedDataAccess.Client;
namespace ODP.NET_Autonomous
{
class Program
{
static void Main()
{
//Enter your ADB's user id, password, and net service name
string conString = "User Id=<ADMIN>;Password=<MYPASSWORD>;Data Source=<mydb_high>;Connection Timeout=30;";
//Enter directory where you unzipped your cloud credentials
OracleConfiguration.TnsAdmin = @"<MYDIRECTORY>";
OracleConfiguration.WalletLocation = OracleConfiguration.TnsAdmin;
using (OracleConnection con = new OracleConnection(conString))
{
using (OracleCommand cmd = con.CreateCommand())
{
try
{
con.Open();
Console.WriteLine("Successfully connected to Oracle Autonomous Database");
Console.WriteLine();
cmd.CommandText = "select CUST_FIRST_NAME, CUST_LAST_NAME, CUST_CITY, CUST_CREDIT_LIMIT " +
"from SH.CUSTOMERS order by CUST_ID fetch first 20 rows only";
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1) + " in " +
reader.GetString(2) + " has " + reader.GetInt16(3) + " in credit." );
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
}
Note: If you connect to ADB from behind a firewall, you will likely encounter a connection timeout error. Set your ODP.NET network configuration to use an HTTP proxy to connect successfully to ADB. Learn how to do this in this ADB documentation section. Scroll down to the "Connections with an HTTP Proxy" section on the doc page.