All Products
Search
Document Center

PolarDB:.NET

Last Updated:Aug 26, 2026

This topic describes how to use the PolarDB .NET driver to connect your C# application to a PolarDB for PostgreSQL (Compatible with Oracle) database.

Prerequisites

  • You have created a database account in the PolarDB cluster. For more information, see Create a database account.

  • The IP addresses of the hosts that need to access the PolarDB cluster have been added to a whitelist. For more information, see Set a cluster whitelist.

Background information

PolarDB .NET, also known as the ADO.NET Data Provider for PolarDB, is a driver that allows applications written in C#, Visual Basic, or F# to access PolarDB. The driver is compatible with Entity Framework Core and Entity Framework 6.x, enabling rapid application development with Entity Framework.

The current driver uses the PostgreSQL 3.0 protocol and is compatible with .NET Framework 2.0, .NET Framework 4.0, .NET Framework 4.5, and .NET Core 2.0.

In earlier versions of the PolarDB .NET driver, many types within the driver were prefixed with POLARDB. These have now been changed to PolarDB. To update your code, perform a text replacement. The underlying logic of the driver has not changed, so you can upgrade with confidence.

Entity Framework

Entity Framework is a popular object-relational mapping (ORM) framework for the .NET platform. When you write back-end applications in C#, Entity Framework and Language-Integrated Query (LINQ) can significantly accelerate development.

The PolarDB .NET driver provides DLLs that support Entity Framework 5 (EF5) and Entity Framework 6 (EF6).

For more information about Entity Framework, see the official Entity Framework website.

Install the .NET driver

  1. Download the .NET driver.

  2. Unzip the .NET driver package.

    unzip polardb_oracle_.net.zip
  3. Import the driver into your Visual Studio project.

    1. In the Visual Studio UI, right-click your project and select Add Reference.

    2. In the Reference Manager dialog box, click Browse.

      Use the Browse... button to select the files to reference.

    3. In the Select the files to reference dialog box, select the appropriate driver version and click Add.

      For example, if you are using .NET Framework 4.5 (the net45 folder), you must reference four DLL files: PolarDB.PolarDBClient.dll, System.Runtime.CompilerServices.Unsafe.dll, System.Threading.Tasks.Extensions.dll, and System.ValueTuple.dll.

    4. Click OK.

Example

Follow these steps to run the sample projects.

  1. Connect to the database cluster. For more information, see Connect to a database cluster.

  2. Run the following command to create a database named sampledb.

    CREATE DATABASE sampledb;
  3. Import the tables, data, and functions required for the sample into the sampledb database.

    \i ${your path}/PolarDBSample.sql
  4. After the data is imported, you can start to write your C# code.

    Below is an example of how to query, update, and call stored procedures.

    using System;
    using System.Data;
    using PolarDB.PolarDBClient;
    /*
     * This class provides a simple way to perform DML operations in PolarDB
     *
     * @revision 1.0
     */
    namespace PolarDBClientTest
    {
        class SAMPLE_TEST
        {
            static void Main(string[] args)
            {
                PolarDBConnection conn = new PolarDBConnection("Server=localhost;Port=5432;User Id=polaruser;Password=password;Database=sampledb");
                try
                {
                    conn.Open();
                    //Simple select statement using PolarDBCommand object
                    PolarDBCommand PolarDBSeletCommand = new PolarDBCommand("SELECT EMPNO,ENAME,JOB,MGR,HIREDATE FROM EMP",conn);
                    PolarDBDataReader SelectResult =  PolarDBSeletCommand.ExecuteReader();
                    while (SelectResult.Read()) 
                    {
                        Console.WriteLine("Emp No" + " " + SelectResult.GetInt32(0));
                        Console.WriteLine("Emp Name" + " " + SelectResult.GetString(1));
                        if (SelectResult.IsDBNull(2) == false)
                            Console.WriteLine("Job" + " " + SelectResult.GetString(2));
                        else
                            Console.WriteLine("Job" + " null ");
                        if (SelectResult.IsDBNull(3) == false)
                            Console.WriteLine("Mgr" + " " + SelectResult.GetInt32(3));
                        else
                            Console.WriteLine("Mgr" + "null");
                        if (SelectResult.IsDBNull(4) == false)
                            Console.WriteLine("Hire Date" + " " + SelectResult.GetDateTime(4));
                        else
                            Console.WriteLine("Hire Date" + " null");
                        Console.WriteLine("---------------------------------");
                    }
                    //Insert statement using PolarDBCommand Object
                    SelectResult.Close();
                    PolarDBCommand PolarDBInsertCommand = new PolarDBCommand("INSERT INTO EMP(EMPNO,ENAME) VALUES((SELECT COUNT(EMPNO) FROM EMP),'JACKSON')",conn);
                    PolarDBInsertCommand.ExecuteScalar();
                    Console.WriteLine("Record inserted");
                    //Update  using PolarDBCommand Object
                    PolarDBCommand  PolarDBUpdateCommand = new PolarDBCommand("UPDATE EMP SET ENAME ='DOTNET' WHERE EMPNO < 100",conn);
                    PolarDBUpdateCommand.ExecuteNonQuery();
                    Console.WriteLine("Record has been updated");
                    PolarDBCommand PolarDBDeletCommand = new PolarDBCommand("DELETE FROM EMP WHERE EMPNO < 100",conn);
                    PolarDBDeletCommand.CommandType= CommandType.Text;
                    PolarDBDeletCommand.ExecuteScalar();
                    Console.WriteLine("Record deleted");
                    //procedure call example
                    try
                    {
                        PolarDBCommand callable_command = new PolarDBCommand("emp_query(:p_deptno,:p_empno,:p_ename,:p_job,:p_hiredate,:p_sal)", conn);
                        callable_command.CommandType = CommandType.StoredProcedure;
                        callable_command.Parameters.Add(new PolarDBParameter("p_deptno",PolarDBTypes.PolarDBDbType.Numeric,10,"p_deptno",ParameterDirection.Input,false ,2,2,System.Data.DataRowVersion.Current,20));
                        callable_command.Parameters.Add(new PolarDBParameter("p_empno", PolarDBTypes.PolarDBDbType.Numeric,10,"p_empno",ParameterDirection.InputOutput,false ,2,2,System.Data.DataRowVersion.Current,7369));
                        callable_command.Parameters.Add(new PolarDBParameter("p_ename", PolarDBTypes.PolarDBDbType.Varchar,10,"p_ename",ParameterDirection.InputOutput,false ,2,2,System.Data.DataRowVersion.Current,"SMITH"));
                        callable_command.Parameters.Add(new PolarDBParameter("p_job", PolarDBTypes.PolarDBDbType.Varchar,10,"p_job",ParameterDirection.Output,false ,2,2,System.Data.DataRowVersion.Current,null));
                        callable_command.Parameters.Add(new PolarDBParameter("p_hiredate", PolarDBTypes.PolarDBDbType.Date,200,"p_hiredate",ParameterDirection.Output,false ,2,2,System.Data.DataRowVersion.Current,null));
                        callable_command.Parameters.Add(new PolarDBParameter("p_sal", PolarDBTypes.PolarDBDbType.Numeric,200,"p_sal",ParameterDirection.Output,false ,2,2,System.Data.DataRowVersion.Current,null));
                        callable_command.Prepare();
                        callable_command.Parameters[0].Value = 20;
                        callable_command.Parameters[1].Value = 7369;
                        PolarDBDataReader result = callable_command.ExecuteReader();
                        int fc = result.FieldCount;
                        for(int i=0;i<fc;i++)
                            Console.WriteLine("RESULT["+i+"]="+ Convert.ToString(callable_command.Parameters[i].Value));
                        result.Close();
                    }
                    // If you are using the .NET 2.0 driver, you may need to modify this section.
                    catch(PolarDBException exp)
                    {
                        if(exp.ErrorCode.Equals("01403"))
                            Console.WriteLine("No data found");
                        else if(exp.ErrorCode.Equals("01422"))
                            Console.WriteLine("More than one row was returned by the query");
                        else
                            Console.WriteLine("There was an error calling the procedure. \nRoot Cause:\n");
                        Console.WriteLine(exp.Message.ToString());
                    }
                    //Prepared statement
                    string updateQuery  = "update emp set ename = :Name where empno = :ID";
                    PolarDBCommand Prepared_command = new PolarDBCommand(updateQuery, conn);
                    Prepared_command.CommandType = CommandType.Text;
                    Prepared_command.Parameters.Add(new PolarDBParameter("ID", PolarDBTypes.PolarDBDbType.Integer));
                    Prepared_command.Parameters.Add(new PolarDBParameter("Name", PolarDBTypes.PolarDBDbType.Text));
                    Prepared_command.Prepare();
                    Prepared_command.Parameters[0].Value = 7369;
                    Prepared_command.Parameters[1].Value = "Mark";
                    Prepared_command.ExecuteNonQuery();
                    Console.WriteLine("Record Updated...");
                }
                catch(PolarDBException exp)
                {
                    Console.WriteLine(exp.ToString() );
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }

Connection string parameters

When your application connects to a database, it must provide a connection string that contains parameters such as the host, username, and password.

The format for a connection string is keyword1=value; keyword2=value;. Keywords are not case-sensitive. Enclose values that contain special characters, such as a semicolon, in double quotation marks ("").

This driver supports the following connection string parameters.

Table 1. Basic connection

Parameter

Example

Description

Host

localhost

The endpoint of the PolarDB cluster. For more information, see View or apply for an endpoint.

Port

1521

The default port for a PolarDB cluster is 1521.

Database

sampledb

The name of the database to connect to.

Username

polaruser

The username for the PolarDB cluster.

Password

password

The password for the PolarDB cluster username.

Table 2. Connection pool

Parameter

Example

Description

Pooling

true

Specifies whether to enable the connection pool.

Minimum Pool Size

0

The minimum number of connections to keep in the pool.

Maximum Pool Size

100

The maximum number of connections allowed in the pool.

Connection Idle Lifetime

300

The timeout in seconds. After this period, idle connections are closed if the pool size exceeds the Minimum Pool Size.

Connection Pruning Interval

10

The interval in seconds at which idle connections are pruned from the pool.

Table 3. Other parameters

Parameter

Description

Application name

The application name.

Search path

The schema search path.

Client encoding

The client encoding.

Time zone

The time zone for the session.