Introduction
In this article, we will see how to create an Oracle database instance in Amazon RDS (AWS Relational Database Service) and we will connect to the Oracle database from the Blazor application. We will use the Employee details Single Page Application for this purpose.
Prerequisites
- AWS Account
- Visual Studio 2017 (any edition) with .NET SDK 2.1 or later
- Blazor Language Support
Please refer to my C# Corner article to know the details about AWS free tier account creation.
If you are new to the Blazor framework, please refer to my below articles in C# Corner to learn the basics of the Blazor framework.
- Single Page Application With Blazor And CosmosDB
- Blazor - CRUD Using PostgreSQL And Entity Framework Core
- Blazor - Connect With Amazon DynamoDB Using AWS SDK
- Blazor - Work With Cassandra API In Cosmos DB
- Localization In Blazor App Using Microsoft.JSInterop
Create an Oracle Database instance in Amazon RDS
Login to the AWS console
Select Database -> RDS -> Create database -> Oracle

Choose the Oracle Edition. If you are using the AWS free tier, please choose Only enable options eligible for the RDS Free Usage Tier and click the “Next” button to proceed.

Give Oracle DB instance and master username and password details.

Give the Database options. Here you must provide the default database name and port details. By default, the database name is “ORCL” and the Port is “1521”. You can proceed with all the default values.

Please choose the “Enable deletion protection” option if you want to avoid the database being deleted accidentally. You can’t delete the database if you select this option. Please click the “Create database” button to proceed.

You will be notified with a message as shown below. You can click “View DB instance details” to get the database creation details.
You can see that the DB instance status is “creating”. It will take some time to finish the instance creation.

After some time, our database is created successfully. We can change the inbound IP rule. So that database can be accessed from outside AWS.

In my system IP address often changes. So, I will change the rule to Anywhere. So that I can access the Oracle database from any IP address. It is not suggested for the production database.

Connect Oracle Database with Oracle SQL Developer
We have successfully created Oracle database and modified the inbound IP rule. Now we can connect the Oracle database from Oracle SQL Developer tool. Oracle SQL Developer tool is a fee IDE for Oracle administration developed by Oracle corporation.
You can click the “+” to add a new connection.

We can give the Oracle instance details to create a new connection. You can get the Hostname from AWS console.

We have successfully connected to Oracle database. Now we can create an Employee table

CREATE TABLE Employee (
Id VARCHAR2(50) PRIMARY KEY,
Name VARCHAR2(50),
Department VARCHAR2(50),
Designation VARCHAR2(50),
Gender VARCHAR2(10)
);
Create Blazor Application
We can create a Blazor application now. I am creating an Employee data Single Page Application.
Open Visual Studio 2017 and choose .NET Core -> ASP.NET Core Web Application template

Currently there are three types of Blazor templates available. We can choose Blazor (ASP.NET Core hosted) template.

Our solution will be ready in a moment. Please note that there are three projects created in our solution - “Client”, “Server”, and “Shared”.

By default, Blazor created many files in these three projects. We can remove all the unwanted files like “Counter.cshtml”, “FetchData.cshtml”, “SurveyPrompt.cshtml” from Client project and “SampleDataController.cs” file from Server project and delete “WeatherForecast.cs” file from shared project too.
We can create a new “Models” folder in “Shared” project and add Employee class inside this folder.
Employee.cs
namespace BlazorOracle.Shared.Models
{
public class Employee
{
public string Id
{
get;
set;
}
public string Name
{
get;
set;
}
public string Department
{
get;
set;
}
public string Designation
{
get;
set;
}
public string Gender
{
get;
set;
}
}
}
We will use “Oracle.ManagedDataAccess.Core” NuGet package to connect Oracle database from Blazor application. We can install this package from NuGet manager in “Server” project

We can create a “DataAccess” folder in “Server” project and create “OracleDBContext” class inside this folder. This class contains all the logic for Oracle CRUD operations. We will call all these methods from our API controller later.
Please copy below code and paste to this class.
OracleDBContext.cs
using BlazorOracle.Shared.Models;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
namespace BlazorOracle.Server.DataAccess
{
public class OracleDBContext
{
public string ConnectionString { get; set; }
public OracleDBContext(string connectionString)
{
ConnectionString = connectionString;
}
private OracleConnection GetConnection()
{
return new OracleConnection(ConnectionString);
}
public async Task<List<Employee>> GetAllAsync()
{
List<Employee> list = new List<Employee>();
using (OracleConnection conn = GetConnection())
{
conn.Open();
var commandText = @"SELECT Id,Name,Department,Designation,Gender FROM Employee";
OracleCommand cmd = new OracleCommand(commandText, conn);
using (var reader = cmd.ExecuteReader())
{
while (await reader.ReadAsync())
{
list.Add(new Employee()
{
Id = await reader.GetFieldValueAsync<string>(0),
Name = await reader.GetFieldValueAsync<string>(1),
Department = await reader.GetFieldValueAsync<string>(2),
Designation = await reader.GetFieldValueAsync<string>(3),
Gender = await reader.GetFieldValueAsync<string>(4),
});
}
}
}
return list;
}
public async Task InsertAsync(Employee employee)
{
employee.Id = Guid.NewGuid().ToString();
using (OracleConnection conn = GetConnection())
{
conn.Open();
var commandText = @"INSERT INTO Employee (Id,Name,Department,Designation,Gender) VALUES (:Id, :Name, :Department, :Designation, :Gender)";
OracleCommand cmd = new OracleCommand(commandText, conn);
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Id",
DbType = DbType.String,
Value = employee.Id,
});
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Name",
DbType = DbType.String,
Value = employee.Name,
});
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Department",
DbType = DbType.String,
Value = employee.Department,
});
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Designation",
DbType = DbType.String,
Value = employee.Designation,
});
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Gender",
DbType = DbType.String,
Value = employee.Gender,
});
await cmd.ExecuteNonQueryAsync();
}
}
public async Task UpdateAsync(Employee employee)
{
using (OracleConnection conn = GetConnection())
{
conn.Open();
var commandText = @"UPDATE Employee SET Name=:Name, Department=:Department, Designation=:Designation, Gender=:Gender Where Id=:Id";
OracleCommand cmd = new OracleCommand(commandText, conn);
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Name",
DbType = DbType.String,
Value = employee.Name,
});
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Department",
DbType = DbType.String,
Value = employee.Department,
});
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Designation",
DbType = DbType.String,
Value = employee.Designation,
});
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Gender",
DbType = DbType.String,
Value = employee.Gender,
});
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Id",
DbType = DbType.String,
Value = employee.Id,
});
await cmd.ExecuteNonQueryAsync();
}
}
public async Task DeleteAsync(string id)
{
using (OracleConnection conn = GetConnection())
{
conn.Open();
var commandText = @"DELETE FROM Employee Where Id=:Id";
OracleCommand cmd = new OracleCommand(commandText, conn);
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Id",
DbType = DbType.String,
Value = id,
});
await cmd.ExecuteNonQueryAsync();
}
}
public async Task<Employee> FindOneAsync(string id)
{
using (OracleConnection conn = GetConnection())
{
conn.Open();
var commandText = @"SELECT Name,Department,Designation,Gender FROM Employee Where Id=:Id";
OracleCommand cmd = new OracleCommand(commandText, conn);
cmd.Parameters.Add(new OracleParameter
{
ParameterName = "@Id",
DbType = DbType.String,
Value = id,
});
using (var reader = cmd.ExecuteReader())
{
if (await reader.ReadAsync())
{
return new Employee()
{
Id = id,
Name = await reader.GetFieldValueAsync<string>(0),
Department = await reader.GetFieldValueAsync<string>(1),
Designation = await reader.GetFieldValueAsync<string>(2),
Gender = await reader.GetFieldValueAsync<string>(3),
};
}
else
{
return null;
}
}
}
}
}
}








Join the conversation! Your thoughts help the community grow.