DataStax C# driver

Before using the DataStax driver, review the Best practices for DataStax drivers to understand the rules and recommendations for improving performance and minimizing resource utilization in applications that use a DataStax driver.

DataStax recommends using the unified DataStax driver. See this blog post for more information. You can also use the DataStax Enterprise (DSE) drivers, which exposes the same API for connecting to Cassandra databases.

Prerequisites

Downloading secure connect bundle

To connect to your Astra DB database using the drivers, download the secure database bundle from the DataStax Astra DB console that contains the connection credentials.

  1. Open a browser, navigate to Astra DB, and log in.

  1. From your Dashboard page, select your database.

  1. On the Overview page, select Connect.

  1. In the Connect using a driver section, click your preferred language from the list to load language-specific instructions.

    If you have multiple regions, select the region you want to connect to from the dropdown menu for instructions.

Note, though, that the bundle URL is the same for all languages.

  1. Click Download Bundle.

    If you have multiple regions, you will have the option to download a bundle for each region from the expanded Download Bundle drop-down menu.

If you’ve enabled VPC peering, you can also Download External Secure Connect Bundle for use within your VPC peering. The secure-connect-database_name.zip file downloads, which contains the security certificates and credentials for your database.

VPC peering is only available on Classic databases.

Alternatively, you can right-click the Download credentials link, copy the link source, and then use a curl command to download the secure-connect-database_name.zip file:

curl -L "<secure-connect-database_name-download-URL>" > database_name.zip

Sharing secure connect bundle

Although teammates can access your Astra DB database, it will not display in their list of available databases under My Databases in the Astra DB console.

After you create an Astra DB database, you can grant access to other members of your team by providing them with the database credentials and connection details for your database.

Be careful when sharing connection details. Providing this information to another user grants them access to your Astra DB database and ownership capabilities, such as making modifications to the database.

For security, delete downloaded connection credentials after sending them to your teammate.

Connecting C# driver

  1. Create a new C# project and configure it to connect to your Cassandra database.

mkdir csharpproject
cd csharpproject
dotnet new console
  1. Add the dependencies for the C# driver to your project.

dotnet add package CassandraCSharpDriver -v 3.15.0
  1. Replace the code in Program.cs with the following code to connect to your Astra DB database.

Include the absolute path to the secure connect bundle for your Astra DB database (secure-connect-database_name.zip) in the WithCloudSecureConnectionBundle method call, and your credentials in the WithCredentials method call, as shown in the following examples.

using System;
using System.Linq;
using Cassandra;

namespace csharpproject
{
    class Program
    {
        static void Main(string[] args)
        {
            var session =
                Cluster.Builder()
                       .WithCloudSecureConnectionBundle(@"C:\path\to\secure-connect-database_name.zip")
                       .WithCredentials("clientId", "clientSecret")
                       .Build()
                       .Connect();
        }
    }
}
  1. After the connection code, add the following code to the Main method in Program.cs. This code runs a CQL query, and prints the output to the console.

var rowSet = session.Execute("select * from system.local");
Console.WriteLine(rowSet.First().GetValue<string>("cluster_name"));
  1. Run your C# project with the dotnet runtime.

dotnet restore
dotnet build
dotnet run --no-build

Migrating C# driver

Complete the following procedure to migrate your existing DataStax C# driver to a version that is capable of connecting to Astra databases created using DataStax Astra DB.

  1. Add the dependencies for the C# driver to your project.

    • DataStax C# driver for Apache Cassandra

    • DSE C# driver

    dotnet add package CassandraCSharpDriver -v 3.15.0
    dotnet add package Dse -v 3.15.0
  2. In your existing DataStax C# driver code, modify the connection code to use the Astra DB API.

    Include the absolute path to the secure connect bundle for your Cassandra database (secure-connect-database_name.zip) in the WithCloudSecureConnectionBundle method call, and your credentials in the WithCredentials method call, as shown in the following examples.

    • DataStax C# driver for Apache Cassandra

    • DSE C# driver

    var session =
      Cluster.Builder()
          .WithCloudSecureConnectionBundle(@"C:\path\to\secure-connect-database_name.zip")
            .WithCredentials("clientId", "clientSecret")
            .Build()
            .Connect();
    var session =
      DseCluster.Builder()
          .WithCloudSecureConnectionBundle(@"C:\path\to\secure-connect-database_name.zip")
            .WithCredentials("clientId", "clientSecret")
            .Build()
            .Connect();
  3. Run your application.