These tutorials show you how to connect .NET applications to Oracle Database (On-Premises) 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 Oracle Database (On-Premises) using Visual Studio and Oracle Data Provider for .NET. Visit Developing .NET Applications for Oracle Autonomous Database for a tutorial using Oracle Autonomous Database.
This tutorial shows you how use the command line to connect .NET Core applications to Oracle Databases using Oracle Data Provider for .NET (ODP.NET) Core. Follow the steps below:
If you do not already have access to an Oracle Database, click the links below to install Oracle Database Express Edition (XE). (If you would like to use the Always Free Oracle Autonomous Database instead, see the Developing .NET Applications for Oracle Autonomous Database tutorial).
We recommend using a new database user for this app. We will store the To Do table in the new user's schema. If you are not familiar with creating a user and assigning privileges, run the Create User ODP.NET Core app below. This app creates a new database user on behalf of a database administrator from the command line. This tool is intended for on-premises databases or cloud databases with TCP connections. It requires .NET Core 3.1 or higher.
Alternatively, you may run the sample code in Step 4 from an existing user schema if you do not wish to create a new user.
dotnet new console
dotnet add package Oracle.ManagedDataAccess.Core
using System;
using Oracle.ManagedDataAccess.Client;
namespace Create_User
{
class Program
{
//Set the net service name, Easy Connect, or connect descriptor of the pluggable DB,
// such as "localhost/XEPDB1" for 18c XE or higher
public static string db = "<localhost/XEPDB1>";
//Provide the DBA's user id, password, and role to create the demo user
//If admin has no role, then set to empty string
public static string sysUser = "<SYS>";
public static string sysPwd = "<Admin PASSWORD>";
public static string role = "<SYSDBA>";
//Set the demo user id, such as DEMODOTNET and password
public static string user = "<DEMODOTNET>";
public static string pwd = "<PASSWORD>";
static void Main()
{
//Create connection string. Check whether DBA Privilege is required.
string conStringDBA;
if (role == "")
conStringDBA = "User Id=" + sysUser + ";Password=" + sysPwd + ";Data Source=" + db + ";";
else
conStringDBA = "User Id=" + sysUser + ";Password=" + sysPwd + ";DBA Privilege=" + role + ";Data Source=" + db + ";";
using (OracleConnection con = new OracleConnection(conStringDBA))
{
using (OracleCommand cmd = con.CreateCommand())
{
try
{
con.Open();
Console.WriteLine("Successfully connected to Oracle Database");
Console.WriteLine();
//Modify the anonymous PL/SQL GRANT command if you wish to modify the privileges granted
cmd.CommandText = "BEGIN " +
"EXECUTE IMMEDIATE ('CREATE USER " + user + " IDENTIFIED BY " + pwd +
" DEFAULT TABLESPACE USERS QUOTA UNLIMITED ON USERS'); " +
"EXECUTE IMMEDIATE ('GRANT CREATE SESSION, CREATE VIEW, CREATE SEQUENCE, CREATE PROCEDURE, " +
"CREATE TABLE, CREATE TRIGGER, CREATE TYPE, CREATE MATERIALIZED VIEW TO " + user + "'); " +
"END;";
cmd.ExecuteNonQuery();
Console.WriteLine(user + " user created");
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
}
dotnet run
We are ready to setup a simple database schema, add data, and then use a .NET Core app to select results from a table. We will create a basic To Do list app. The To Do items will be stored in a database table that will track a task list and task completion status.
dotnet new console
dotnet add package Oracle.ManagedDataAccess.Core
using System;
using System.Data;
using Oracle.ManagedDataAccess.Client;
namespace ODP.NET_Core_Get_Started
{
class GettingStarted
{
//Prerequisite: This app assumes the user has already been created with the
// necessary privileges
//Set the demo user id, such as DEMODOTNET and password
public static string user = "<DEMODOTNET>";
public static string pwd = "<PASSWORD>";
//Set the net service name, Easy Connect, or connect descriptor of the pluggable DB,
// such as "localhost/XEPDB1" for 18c XE or higher
public static string db = "<localhost/XEPDB1>";
static void Main()
{
string conStringUser = "User Id=" + user + ";Password=" + pwd + ";Data Source=" + db + ";";
using (OracleConnection con = new OracleConnection(conStringUser))
{
using (OracleCommand cmd = con.CreateCommand())
{
try
{
con.Open();
Console.WriteLine("Successfully connected to Oracle Database as " + user);
Console.WriteLine();
//Create the sample table. Checks if table already exists
cmd.CommandText = "DECLARE tableExists INTEGER; " +
"BEGIN " +
"SELECT count(*) into tableExists FROM user_tables where table_name = 'TODOITEM';" +
"IF (tableExists = 0) THEN " +
"EXECUTE IMMEDIATE ('CREATE TABLE todoitem (id NUMBER GENERATED ALWAYS AS IDENTITY, " +
"description VARCHAR2(4000), creation_ts TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, " +
"done NUMBER(1, 0), PRIMARY KEY(id))'); " +
"END IF; END;";
cmd.ExecuteNonQuery();
//Insert sample data
String[] description = { "Task 1", "Task 2", "Task 3", "Task 4", "Task 5" };
int[] done = new int[5] { 0, 0, 1, 0, 1 };
cmd.CommandText = "INSERT INTO todoitem (description, done) VALUES(:descrip, :done)";
cmd.ArrayBindCount = 5;
cmd.BindByName = true;
OracleParameter descripParam = new OracleParameter("descrip", OracleDbType.Varchar2);
descripParam.Direction = ParameterDirection.Input;
descripParam.Value = description;
cmd.Parameters.Add(descripParam);
OracleParameter doneParam = new OracleParameter("done", OracleDbType.Int16);
doneParam.Direction = ParameterDirection.Input;
doneParam.Value = done;
cmd.Parameters.Add(doneParam);
cmd.ExecuteNonQuery();
Console.WriteLine("{0} Rows Inserted", cmd.ArrayBindCount);
Console.WriteLine();
//Retrieve sample data
cmd.CommandText = "SELECT description, done FROM todoitem";
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (reader.GetBoolean(1))
Console.WriteLine(reader.GetString(0) + " is done.");
else
Console.WriteLine(reader.GetString(0) + " is NOT done.");
}
descripParam.Dispose();
doneParam.Dispose();
reader.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
}
dotnet run
This tutorial shows you how to connect .NET Core applications to Oracle Databases 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:
If you do not already have access to an Oracle Database, click the links below to install Oracle Database Express Edition (XE). (If you would like to use the Always Free Oracle Autonomous Database instead, see the Developing .NET Applications for Oracle Autonomous Database tutorial).
We recommend using a new database user with the app we will create in this walkthrough. We will store a "To Do" table in the new user's schema. If you are not familiar with creating a user and assigning privileges, follow the steps below to use Visual Studio Code to log in as a database administrator and execute the SQL needed to create a new database user.
Alternatively, you may skip to Step 5 and use an existing user schema if you do not wish to create a new user.
CREATE USER DEMODOTNET IDENTIFIED BY PASSWORD
DEFAULT TABLESPACE USERS QUOTA UNLIMITED ON USERS;
GRANT CREATE SESSION, CREATE VIEW, CREATE SEQUENCE,
CREATE PROCEDURE, CREATE TABLE, CREATE TRIGGER,
CREATE TYPE, CREATE MATERIALIZED VIEW TO DEMODOTNET;
CREATE TABLE TODOITEM (id NUMBER GENERATED ALWAYS AS IDENTITY,
DESCRIPTION VARCHAR2(4000), CREATION_TS TIMESTAMP WITH TIME ZONE
DEFAULT CURRENT_TIMESTAMP, DONE NUMBER(1, 0), PRIMARY KEY(id));
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 1', 0);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 2', 0);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 3', 1);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 4', 0);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 5', 1);
COMMIT;
SELECT DESCRIPTION, DONE FROM TODOITEM;
dotnet new console
dotnet add package Oracle.ManagedDataAccess.Core
using System;
using System.Data;
using Oracle.ManagedDataAccess.Client;
namespace ODP.NET_Core_Get_Started
{
class GettingStarted
{
//Prerequisite: This app assumes the user has already been created with the
// necessary privileges
//Set the demo user id, such as DEMODOTNET and password
public static string user = "<DEMODOTNET>";
public static string pwd = "<PASSWORD>";
//Set the net service name, Easy Connect, or connect descriptor of the pluggable DB,
// such as "localhost/XEPDB1" for 18c XE or higher
public static string db = "<localhost/XEPDB1>";
static void Main()
{
string conStringUser = "User Id=" + user + ";Password=" + pwd + ";Data Source=" + db + ";";
using (OracleConnection con = new OracleConnection(conStringUser))
{
using (OracleCommand cmd = con.CreateCommand())
{
try
{
con.Open();
Console.WriteLine("Successfully connected to Oracle Database as " + user);
Console.WriteLine();
//Retrieve sample data
cmd.CommandText = "SELECT description, done FROM todoitem";
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (reader.GetBoolean(1))
Console.WriteLine(reader.GetString(0) + " is done.");
else
Console.WriteLine(reader.GetString(0) + " is NOT done.");
}
reader.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
}
dotnet run
This tutorial shows you how to connect .NET Core applications to Oracle Database (on-premises) 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 do not already have access to an Oracle Database, click the links below to install Oracle Database Express Edition (XE). (If you would like to use the Always Free Oracle Autonomous Database instead, see the Developing .NET Applications for Oracle Autonomous Database tutorial).
We recommend using a new database user with the app we will create in this walkthrough. We will store a "To Do" table in the new user's schema. If you are not familiar with creating a user and assigning privileges, follow the steps below to use Visual Studio to log in as a database administrator and execute the SQL needed to create a new database user.
Alternatively, you may skip to the next step (Step 4: Connect to Oracle Database) and use an existing user schema if you do not wish to create a new user.
CREATE USER DEMODOTNET IDENTIFIED BY PASSWORD
DEFAULT TABLESPACE USERS QUOTA UNLIMITED ON USERS;
GRANT CREATE SESSION, CREATE VIEW, CREATE SEQUENCE,
CREATE PROCEDURE, CREATE TABLE, CREATE TRIGGER,
CREATE TYPE, CREATE MATERIALIZED VIEW TO DEMODOTNET;
CREATE TABLE TODOITEM (id NUMBER GENERATED ALWAYS AS IDENTITY,
DESCRIPTION VARCHAR2(4000), CREATION_TS TIMESTAMP WITH TIME ZONE
DEFAULT CURRENT_TIMESTAMP, DONE NUMBER(1, 0), PRIMARY KEY(id));
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 1', 0);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 2', 0);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 3', 1);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 4', 0);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 5', 1);
COMMIT;
SELECT DESCRIPTION, DONE FROM TODOITEM;
In this step you will build a .NET Core application that can run on Windows, Linux, and macOS.
using System;
using System.Data;
using Oracle.ManagedDataAccess.Client;
namespace ODP.NET_Core_Get_Started
{
class GettingStarted
{
//Prerequisite: This app assumes the user has already been created with the
// necessary privileges
//Set the demo user id, such as DEMODOTNET and password
public static string user = "<DEMODOTNET>";
public static string pwd = "<PASSWORD>";
//Set the net service name, Easy Connect, or connect descriptor of the pluggable DB,
// such as "localhost/XEPDB1" for 18c XE or higher
public static string db = "<localhost/XEPDB1>";
static void Main()
{
string conStringUser = "User Id=" + user + ";Password=" + pwd + ";Data Source=" + db + ";";
using (OracleConnection con = new OracleConnection(conStringUser))
{
using (OracleCommand cmd = con.CreateCommand())
{
try
{
con.Open();
Console.WriteLine("Successfully connected to Oracle Database as " + user);
Console.WriteLine();
//Retrieve sample data
cmd.CommandText = "SELECT description, done FROM todoitem";
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (reader.GetBoolean(1))
Console.WriteLine(reader.GetString(0) + " is done.");
else
Console.WriteLine(reader.GetString(0) + " is NOT done.");
}
reader.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
}
This tutorial shows you how to connect .NET Framework applications to Oracle Database (on-premises) 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 do not already have access to an Oracle Database, click the links below to install Oracle Database Express Edition (XE). (If you would like to use the Always Free Oracle Autonomous Database instead, see the Developing .NET Applications for Oracle Autonomous Database tutorial).
We recommend using a new database user with the app we will create in this walkthrough. We will store a "To Do" table in the new user's schema. If you are not familiar with creating a user and assigning privileges, follow the steps below to use Visual Studio to log in as a database administrator and execute the SQL needed to create a new database user.
Alternatively, you may skip to the next step (Step 4: Connect to Oracle Database) and use an existing user schema if you do not wish to create a new user.
CREATE USER DEMODOTNET IDENTIFIED BY PASSWORD
DEFAULT TABLESPACE USERS QUOTA UNLIMITED ON USERS;
GRANT CREATE SESSION, CREATE VIEW, CREATE SEQUENCE,
CREATE PROCEDURE, CREATE TABLE, CREATE TRIGGER,
CREATE TYPE, CREATE MATERIALIZED VIEW TO DEMODOTNET;
CREATE TABLE TODOITEM (id NUMBER GENERATED ALWAYS AS IDENTITY,
DESCRIPTION VARCHAR2(4000), CREATION_TS TIMESTAMP WITH TIME ZONE
DEFAULT CURRENT_TIMESTAMP, DONE NUMBER(1, 0), PRIMARY KEY(id));
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 1', 0);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 2', 0);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 3', 1);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 4', 0);
INSERT INTO TODOITEM (DESCRIPTION, DONE) VALUES('Task 5', 1);
COMMIT;
SELECT DESCRIPTION, DONE FROM TODOITEM;
In this step you will build a .NET Framework application.
using System;
using System.Data;
using Oracle.ManagedDataAccess.Client;
namespace ODP.NET_Framework_Get_Started
{
class GettingStarted
{
//Prerequisite: This app assumes the user has already been created with the
// necessary privileges
//Set the demo user id, such as DEMODOTNET and password
public static string user = "<DEMODOTNET>";
public static string pwd = "<PASSWORD>";
//Set the net service name, Easy Connect, or connect descriptor of the pluggable DB,
// such as "localhost/XEPDB1" for 18c XE or higher
public static string db = "<localhost/XEPDB1>";
static void Main()
{
string conStringUser = "User Id=" + user + ";Password=" + pwd + ";Data Source=" + db + ";";
using (OracleConnection con = new OracleConnection(conStringUser))
{
using (OracleCommand cmd = con.CreateCommand())
{
try
{
con.Open();
Console.WriteLine("Successfully connected to Oracle Database as " + user);
Console.WriteLine();
//Retrieve sample data
cmd.CommandText = "SELECT description, done FROM todoitem";
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (reader.GetBoolean(1))
Console.WriteLine(reader.GetString(0) + " is done.");
else
Console.WriteLine(reader.GetString(0) + " is NOT done.");
}
reader.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
}