これらのチュートリアルでは、Oracle Data Provider for .NET Coreを使用して、コマンドラインおよびVisual Studio環境で.NETアプリケーションをOracle AI Database(オンプレミス)に接続する方法を紹介します。また、Visual StudioとOracle Data Provider for .NET、 Managed Driverを使用して、.NET FrameworkアプリケーションをOracle AI Database(オンプレミス)に接続する方法についても説明します。Oracle Autonomous AI Databaseを使用したチュートリアルについては、Oracle Autonomous AI Databaseのための.NETアプリケーションの開発を参照してください。
このチュートリアルでは、Oracle Data Provider for .NET(ODP.NET)Coreを使用して、コマンドラインから.NETアプリケーションをOracle AI Databaseに接続する方法について説明します。以下の手順に従ってください。
Oracle AI Databaseにまだアクセスできない場合は、以下のリンクをクリックしてOracle AI Database Freeをインストールしてください。代わりにAlways Free Oracle Autonomous Databaseを使用する場合は、Developing .NET Applications for Oracle Autonomous Databaseチュートリアルを参照してください。
このアプリケーションには、新しいデータベース・ユーザーを使用することをお勧めします。新しいユーザーのスキーマに「To Do」表を格納します。ユーザーの作成と権限の割り当てに慣れていない場合は、以下の[Create User ODP.NET Core]アプリを実行してください。このアプリは、データベース管理者に代わってコマンドラインから新しいデータベース・ユーザーを作成します。このツールは、オンプレミス・データベースまたは TCP 接続のクラウド・データベースを対象としています。これには.NETランタイムが必要です。
また、新しいユーザーを作成したくない場合は、既存のユーザースキーマからステップ4のサンプルコードを実行することもできます。
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/FREEPDB1" for Oracle AI Database 26ai Free
public static string db = "<localhost/FREEPDB1>";
//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 AI 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
簡単なデータベース・スキーマをセットアップしてデータを追加し、.NETアプリを使って表から結果を選択する準備が整いました。基本的な作業予定リストアプリを作成します。作業予定品目は、タスクのリストおよびタスクの完了を追跡するデータベース表に格納されます。
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/FREEPDB1" for Oracle AI Database 26ai Free
public static string db = "<localhost/FREEPDB1>";
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 AI 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
このチュートリアルでは、Oracle Data Provider for .NET(ODP.NET)Core、Visual Studio 2026、およびOracle Developer Tools for Visual Studio拡張機能を使用して、.NETアプリケーションをOracle AI Database(オンプレミス)に接続する方法を紹介します。以下の手順に従ってください。
(注: Visual Studioを使用した.NET Frameworkのチュートリアルをすでに完了している場合は、このチュートリアルの最後のステップまでスキップできます。その他のステップは同じです。)
Oracle AI Databaseへのアクセス権をまだお持ちでない場合は、以下のリンクをクリックしてOracle AI Database Freeをインストールしてください。(代わりにAlways Free Oracle Autonomous Databaseを使用する場合は、Developing .NET Applications for Oracle Autonomous Databaseチュートリアルを参照してください。)
このウォークスルーで作成するアプリケーションで、新しいデータベース・ユーザーを使用することをお薦めします。新しいユーザーのスキーマに「To Do」表を格納します。ユーザーの作成と権限の割り当てに慣れていない場合は、以下の手順に従って、Visual Studioを使用してデータベース管理者としてログインし、新しいデータベース・ユーザーを作成するために必要なSQLを実行してください。
また、新しいユーザーを作成したくない場合は、次のステップ(ステップ4: Oracle AI Databaseへの接続)まで進んで、既存のユーザースキーマを使用することもできます。



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;
このステップでは、Windows、LinuxおよびmacOSで実行できる.NETコンソール・アプリケーションを構築します。
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/FREEPDB1" for Oracle AI Database 26ai Free
public static string db = "<localhost/FREEPDB1>";
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 AI 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);
}
}
}
}
}
}

このチュートリアルでは、Oracle Data Provider for .NET(ODP.NET)、Visual Studio 2026、およびOracle Developer Tools for Visual Studio拡張機能を使用して、.NET FrameworkアプリケーションをOracle AI Database(オンプレミス)に接続する方法を説明します。以下の手順に従ってください。
(注: Visual Studioを使用した.NETのチュートリアルをすでに完了している場合は、このチュートリアルの最後のステップまでスキップできます。その他のステップは同じです。)
Oracle AI Databaseへのアクセス権をまだお持ちでない場合は、以下のリンクをクリックしてOracle AI Database Freeをインストールしてください。(代わりにAlways Free Oracle Autonomous Databaseを使用する場合は、Developing .NET Applications for Oracle Autonomous Databaseチュートリアルを参照してください。)
このウォークスルーで作成するアプリケーションで、新しいデータベース・ユーザーを使用することをお薦めします。新しいユーザーのスキーマに「To Do」表を格納します。ユーザーの作成と権限の割り当てに慣れていない場合は、以下の手順に従って、Visual Studioを使用してデータベース管理者としてログインし、新しいデータベース・ユーザーを作成するために必要なSQLを実行してください。
また、新しいユーザーを作成したくない場合は、次のステップ(ステップ4: Oracle AI Databaseへの接続)まで進んで、既存のユーザースキーマを使用することもできます。



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;
このステップでは、.NET Frameworkアプリケーションを構築します。
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/FREEPDB1" for Oracle AI Database 26ai Free
public static string db = "<localhost/FREEPDB1>";
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 AI 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);
}
}
}
}
}
}
