This topic describes how to use the PolarDB .NET driver in a C# application to connect to a 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 ADO.NET Data Provider for PolarDB, is a driver for accessing PolarDB using languages such as C#, Visual Basic, and F#. It is compatible with Entity Framework Core and Entity Framework 6.x. You can use this driver with Entity Framework to accelerate application development.
This driver uses version 3.0 of the PostgreSQL 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 class names started with POLARDB. This prefix has been changed to PolarDB. You must update your code by replacing the old prefix. The driver logic remains unchanged, so you can upgrade with confidence.
Entity Framework
Using Entity Framework and Language-Integrated Query (LINQ) can significantly accelerate the development of C# backend applications.
The PolarDB .NET driver provides the EF5 and EF6 .dll files for use with Entity Framework.
For more information about Entity Framework, see the official Entity Framework website.
Install the .NET driver
Download the .NET driver.
Decompress the .NET driver package.
unzip polardb_oracle_.net.zipImport the driver into your Visual Studio project.
In Visual Studio, right-click the project and click Add Reference.
In the Reference Manager dialog box, click Browse.

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

Click OK.
Example
The Samples directory contains a PolarDBSample.sql file and several sample project files. The following steps show you how to run these sample projects.
Connect to the database. For more information, see Connect to a database cluster.
Run the following command to create a database named
sampledb.CREATE DATABASE sampledb;Import the sample schema and data into the
sampledbdatabase.\i ${your path}/PolarDBSample.sqlAfter the data is imported, you can start writing C# code.
The following sample code shows how to perform queries, updates, and stored procedure calls.
using System; using System.Data; using PolarDB.PolarDBClient; /* * This class demonstrates how 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("---------------------------------"); } // Executes an INSERT statement using a 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"); // Updates a record using a 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 must modify this section accordingly. catch(PolarDBException exp) { if(exp.ErrorCode.Equals("01403")) Console.WriteLine("No data found"); else if(exp.ErrorCode.Equals("01422")) Console.WriteLine("Multiple rows were 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
An application connects to a database by providing a connection string that includes parameters such as the host, username, and password.
A connection string uses the keyword1=value; keyword2=value; format and is not case-sensitive. Use double quotation marks ("") to enclose values that contain special characters, such as a semicolon.
The following tables describe the connection string parameters supported by this driver.
Table 1. Basic connection parameters
Parameter | Example | Description |
Host |
| The endpoint of the PolarDB cluster. For more information about how to view the endpoint, see View or apply for an endpoint. |
Port |
| The port of the PolarDB cluster. The default value is 1521. |
Database |
| The name of the database to connect to. |
Username |
| The username for the PolarDB cluster. |
Password |
| The password for the specified user of the PolarDB cluster. |
Table 2. Connection pool parameters
Parameter | Example | Description |
Pooling |
| Specifies whether to enable the connection pool. |
Minimum pool size | 0 | The minimum number of connections to maintain in the connection pool. |
Maximum pool size | 100 | The maximum number of connections allowed in the connection pool. |
Connection idle lifetime | 300 | The timeout period, in seconds, for closing idle connections when the number of connections exceeds the minimum pool size. |
Connection pruning interval | 10 | The interval, in seconds, for pruning idle connections. |
Table 3. Other parameters
Parameter | Description |
application_name | The name of the application. |
search_path | The schema search path. |
client_encoding | The character encoding used by the client. |
timezone | The time zone for the current session. |