Skip to main content
This tutorial shows you how build a simple C# application with CockroachDB and the .NET Npgsql driver.

Start CockroachDB

Choose whether to run a temporary local cluster or a free CockroachDB cluster on CockroachDB Standard. The instructions below will adjust accordingly.

Create a free trial cluster

  1. . If this is your first CockroachDB Cloud organization, it will be credited with $400 in to get you started.
  2. On the Get Started page, click Create cluster.
  3. On the Select a plan page, select Standard.
  4. On the Cloud & Regions page, select a cloud provider (GCP or AWS).
  5. In the Regions section, select a region for the cluster. Refer to for the regions where CockroachDB Standard clusters can be deployed. To create a multi-region cluster, click Add region and select additional regions.
  6. Click Next: Capacity.
  7. On the Capacity page, keep the at the default value of 2 vCPUs. Click Next: Finalize.
  8. On the Finalize page, name your cluster. If an active free trial is listed in the right pane, you will not need to add a payment method, though you will need to do this by the to maintain your organization’s clusters. Click Create cluster. Your cluster will be created in a few seconds and the Create SQL user dialog will display.

Set up your cluster connection

The Connection info dialog shows information about how to connect to your cluster.
  1. Click the Choose your OS dropdown, and select the operating system of your local machine.
  2. Click the Connection string tab in the Connection info dialog.
  3. Open a new terminal on your local machine, and run the command provided in step 1 to download the CA certificate. This certificate is required by some clients connecting to CockroachDB Cloud.
  4. Copy the connection string provided in step 2 to a secure location.
The connection string is pre-populated with your username, password, cluster name, and other details. Your password, in particular, will be provided only once. Save it in a secure place (Cockroach Labs recommends a password manager) to connect to your cluster in the future. If you forget your password, you can reset it by going to the SQL Users page for the cluster, found at https://cockroachlabs.cloud/cluster/<CLUSTER ID>/users.

Create a .NET project

In your terminal, run the following commands:
The dotnet command creates a new app of type console. The -o parameter creates a directory named cockroachdb-test-app where your app will be stored and populates it with the required files. The cd cockroachdb-test-app command puts you into the newly created app directory.

Install the Npgsql driver

Install the latest version of the Npgsql driver into the .NET project using the built-in nuget package manager:

Create a database

  1. If you haven’t already, .
  2. Start the using the connection string you got from the CockroachDB Cloud Console:
  3. In the SQL shell, create the bank database that your application will use:
  4. Exit the SQL shell:

Set the connection string

Set a DATABASE_URL environment variable to your connection string.

Run the C# code

Now that you have set up your project and created a database, in this section you will:

Basic example

Get the code

Replace the contents of the Program.cs file that was automatically generated in your cockroachdb-test-app directory with the code below:

Run the basic example

Compile and run the code:
The output should be:

Transaction example (with retry logic)

Modify the code

Open cockroachdb-test-app/Program.cs again and replace the contents with the code shown below.

Run the transactions example

This time, running the code will execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted:
The output should be:
However, if you want to verify that funds were transferred from one account to another, use the :

What’s next?

Read more about using the .NET Npgsql driver. You might also be interested in the following pages: