Opening a database connection v9.0.3.1

An EDBConnection object is responsible for handling the communication between an instance of EDB Postgres Advanced Server and a .NET application. Before you can access data stored in an EDB Postgres Advanced Server database, you must create and open an EDBConnection object.

Creating an EDBConnection object

Once you have installed and configured the .NET Connector, you can open a connection using one of the following approaches. In either case, you must import the namespace EnterpriseDB.EDBClient.

Connection with a data source

  1. Create an instance of the EDBDataSource object using a connection string as a parameter to the create method of the EDBDataSource class.

  2. To open a connection, call the OpenConnection method of the EDBDataSource object.

This example shows how to open a connection using a data source:

Connection without a data source

  1. Create an instance of the EDBConnection object using a connection string as a parameter to the constructor of the EDBConnection class.

  2. Call the Open method of the EDBConnection object to open the connection.

Note

For EnterpriseDB.EDBClient 8.0.4 and later, we recommend EDBDataSource to connect to EDB Postgres Advanced Server database or execute SQL directly against it. For more information on the data source, see the Npgsql documentation.

This example shows how to open a connection without a data source:

Connection string parameters

A valid connection string specifies location and authentication information for an EDB Postgres Advanced Server instance. You must provide the connection string before opening the connection. A connection string must contain:

  • The name or IP address of the server
  • The name of the EDB Postgres Advanced Server database
  • The name of an EDB Postgres Advanced Server user
  • The password associated with that user

You can include the following parameters in the connection string:

ParameterDescriptionDefault
Host or ServerThe name or IP address of the EDB Postgres Advanced Server hostRequired
PortThe TCP port of the EDB Postgres Advanced Server host5444
DatabaseThe name of the database to connect to.name of connected user
User Id or UserNameThe username to connect with.OS username
PasswordPassword associated to the user to establish a connection with the serverAuthentication dependent
Command TimeoutSpecifies the length of time (in seconds) to wait for a command to finish executing before throwing an exception.30
PoolingSpecify a value of false to disable connection poolingtrue
No Reset On CloseWhen Pooling is enabled and the connection is closed, reopened, and the underlying connection is reused, then some operations are executed to discard the previous connection resources. You can override this behavior by enabling No Reset On Close.false
SSL ModeControls whether SSL is used, depending on server support.
Prefer — Use SSL if possible. This is the default behavior.
Require — Throw an exception if an SSL connection can't be established.
Allow — Connect without SSL unless server requires it
Disable — Don't attempt an SSL connection.
VerifyCA — SSL with certificate validation
VerifyFull — SSL with certificate validation and host name validation
See Npgsql docs for possible values and more info.
Prefer

For other parameters please refer to the community documentation.

Example: Opening a database connection

This example shows how to open a connection to an instance of EDB Postgres Advanced Server and then close the connection.

In a production application, connection strings should be moved outside of the code, using configuration files for example. See official Microsoft .NET documentation.