This topic describes how to connect to and use OceanBase Database in Oracle mode by using OceanBase Connector/NET.
Prerequisites
You have an accessible OceanBase Database instance.
You have obtained the database account, password, host address, and port number.
You have installed the .NET SDK. This topic provides Linux/macOS commands as an example.
Notice
Before you install .NET 10.0 on Linux, make sure that your system meets the requirements for the glibc version.
Procedure
Step 1: Install the .NET runtime
On Windows, you can download the installation package from the Microsoft website. On Linux/macOS, run the following command:
Note
This topic uses .NET 10.0 as an example. You can choose other supported .NET versions based on your project's compatibility requirements. The installation steps are the same.
wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --channel 10.0
echo 'export PATH="$HOME/.dotnet:$HOME/.dotnet/tools:$PATH"' >> ~/.bashrc
source ~/.bashrc
Verify the installation result:
dotnet --version
If the output is 10.0.0 or a later version, the installation is successful.
Step 2: Create a console project
mkdir dotnet-test
cd dotnet-test
dotnet new console
dotnet run
If the console output is Hello, World!, the project is created successfully.
Step 3: Install OceanBase Connector/NET
After you download the driver to your local computer, run the following command in the project directory to install the driver:
dotnet add <your project.csproj> package Oceanbase.ManagedDataAccess --version 1.0.0 --source "<package repository>" --source "https://api.nuget.org/v3/index.json"
Note
If you use the official NuGet package source, retain --source "https://api.nuget.org/v3/index.json".
You can also modify the nuget.config file. For example:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="local-oceanbase" value="driver download directory" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
After the driver is installed, you can see the following configuration in the .csproj file:
<ItemGroup>
<PackageReference Include="Oceanbase.ManagedDataAccess" Version="1.0.0" />
</ItemGroup>
Step 4: Write sample connection code
Modify the Program.cs file as follows:
using Oceanbase;
namespace Program
{
class Program
{
static void Main(string[] args)
{
OracleConnection conn = new OracleConnection("Server=xxxxx;Port=5858;Database=test;User Id=test@oracle;Password=test;");
try
{
conn.Open();
OracleCommand cmd = new OracleCommand("select to_char(sysdate, 'yyyy-MM-dd HH24:mi:ss') from dual ", conn);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("sysdate is:" + reader.GetString(0));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
}
}
}
Step 5: Run the connection verification
dotnet run
If the current time is output, the connection and query are successful.
Step 6: Connect to the database
After you install the driver, you can run the following sample code to verify the database connectivity. OceanBase Connector/NET uses the Oracle-compatible API and has a similar connection method to Oracle ODP.NET.
using Oceanbase;
var connectionString = "Server=host;Port=port;Database=schema_name;User Id=username@tenant_name#cluster_name;Password=password;";
using var conn = new OracleConnection(connectionString);
conn.Open();
using var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT 1 FROM DUAL";
var result = cmd.ExecuteScalar();
Console.WriteLine($"Query result: {result}");
conn.Close();
The following table describes the parameters.
| Parameter | Description |
|---|---|
| Data Source | The data source address. host or host:port is supported. host:port/servicename is not supported. host is the IP address or host name of an OBServer or ODP node. port is 2881 for direct access or 2883 for access through an ODP node. If you need to specify a service name, specify it in other connection parameters. Do not specify it in the Data Source parameter. |
| User Id | The user identifier. When you connect to OceanBase Database through an ODP node, the user identifier is in the format of username@tenant name#cluster name, which is consistent with the value of the User Id parameter in the connection string. In some cloud scenarios, you can use a shorter format. |
| Password | The database password. |
| Database | Mandatory. In Oracle mode, you must set this parameter to the schema name. Otherwise, an ArgumentException is thrown when you call the Open method. |
References
For more information about the .NET driver, see OceanBase Connector/NET overview.
