This topic describes how to use the PolarDB .NET driver to connect a C# application to a PolarDB for PostgreSQL (Compatible with Oracle) cluster.
Prerequisites
A database account is created for a PolarDB cluster. For information about how to create a database account, see Create a database account.
The IP address of the host that you use to access the PolarDB cluster is added to an IP address whitelist of the cluster. For information about how to add IP addresses to an IP address whitelist of a cluster, see Configure a whitelist for a cluster.
Background information
.NET is a software framework developed by Microsoft that provides a comprehensive programming environment for building and running applications. The first version of the .NET Framework was released in 2002. It provided a unified programming model and supported multiple programming languages. This allows developers to create applications on Windows. The .NET Framework has now been extended to support a variety of platforms, including Linux and macOS.
PolarDB provides .NET programs of various versions, including .NET 2.0, .NET 3.5, .NET 4.0, .NET 5.0, .NET 6.0, .NET 7.0, .NET 8.0, .NET Core 3.1, .NET Standard 2.0, and .NET Standard 2.1.
Software packages
The mappings between Npgsql and PolarDB are complex. PolarDB supports the following versions of Npgsql: 2.2.7, 5.0.10, 7.0.6.1, and 8.0.4.1. PolarDB also supports Entity Framework Core 5.0.10, which provides platform support for .NET versions from 2.0 to 7.0.
Program name | 2.2.7 | 5.0.10 | 7.0.6.1 | 8.0.4.1 |
.NET 2.0 | ||||
.NET 3.5 | ||||
.NET 4.0 | ||||
.NET 5.0 | ||||
.NET 6.0 | ||||
.NET 7.0 | ||||
.NET 8.0 | ||||
.NET Core 3.1 | ||||
.NET Standard 2.0 | ||||
.NET Standard 2.1 |
Connection string parameters
Applications must provide connection strings to connect to databases. The connection strings include parameters such as Host, Username, and Password.
Connection strings are in the keyword1=value; keyword2=value; format and are case-insensitive. You can use double quotation marks (") to enclose the values that contain special characters, such as semicolons (;).
The following table describes the connection string parameters supported by the driver.
Parameter | Example | Description |
Host |
| The endpoint that is used to connect to the PolarDB cluster. For information about how to view the endpoint, see View or apply for an endpoint. |
Port |
| The port number that is used to connect to the PolarDB cluster. Default value: 1521. |
Database |
| The name of the database to which you want to connect. |
Username |
| The name of the database account that is used to connect to the PolarDB cluster. |
Password |
| The password of the database account that is used to connect to the PolarDB cluster. |
Pooling |
| Specifies whether to enable the connection pool feature. |
Minimum Pool Size | 0 | The minimum size of a connection pool. |
Maximum Pool Size | 100 | The maximum size of a connection pool. |
Connection Idle Lifetime | 300 | The timeout period of idle connections. This parameter specifies the duration after which idle connections are closed within the connection pool when the total number of connections exceeds the value of the Minimum Pool Size parameter. Unit: seconds. |
Connection Pruning Interval | 10 | The interval at which idle connections are released. Unit: seconds. |
Example
Sample code:
using Npgsql;
var connString = "Host=xxxx;Port=xxxx;Username=xxx;Password=xxx;Database=xxxx";
await using var conn = new NpgsqlConnection(connString);
await conn.OpenAsync();
// Retrieve all rows
await using (var cmd = new NpgsqlCommand("SELECT sysdate FROM dual", conn))
await using (var reader = await cmd.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
Console.WriteLine(reader.GetDateTime(0));
}Sample result:

FAQ
All table names in an Oracle database are in uppercase. What do I do if the configured table names cannot be found after I migrate the Oracle database to a PolarDB cluster?
Use NuGet to install the
EFCore.NamingConventionspackage in your application. Then, addUseSnakeCaseNamingConvention()to the DbContext configuration. This way, the table names and column names are automatically converted to snake case. This eliminates the need for explicit annotations.protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .UseNpgsql(...) .UseSnakeCaseNamingConvention();Specific custom type conversions in my application use the Date type. What do I do if an error occurs after I use the PolarDB package?
If the NpgsqlDbType.Date type is used at the data layer, you can change the type to NpgsqlDbType.Sysdate.
Before the modification:
case OracleDbType.Date: return NpgsqlDbType.Date;After the modification:
case OracleDbType.Date: return NpgsqlDbType.Sysdate;
Release notes
8.0.4.1 (September 18, 2024)
New feature: PolarDB 8.0.4.1 is supported. The PolarDB version is compatible with the
.NET 6.0,.NET 7.0, and.NET 8.0versions.
7.0.6.1 (August 23, 2024)
New feature: PL/SQL stored procedures can be executed without specifying the double dollar signs (
$$).Fixed issues:
The built-in objects of the database management system (DBMS) in metadata are not correctly identified.
Data types cannot be read due to
ROWID.
7.0.6 (June 19, 2024)
New feature: PolarDB for PostgreSQL (Compatible with Oracle) 2.0 supports 64-bit date formats.