Connect with the C# driver

Because Astra DB is based on Apache Cassandra®, you can use Cassandra drivers to connect to your Astra DB Classic databases.

To use the C# driver, you need to install the driver and its dependencies, and then connect the driver to your Astra DB Classic database. Once connected, you can write scripts that use the driver to run commands against your database.

Prerequisites

  1. Download and install the current version of the Microsoft .NET Core SDK.

  2. Create a database.

  3. Download your database’s Secure Connect Bundle (SCB).

    For multi-region databases, download the Secure Connect Bundle (SCB) for a region that is geographically close to your application to reduce latency.

    If you need to connect to multiple regions in the same application, you need the Secure Connect Bundle (SCB) for each region, and your driver code must instantiate one root object (session) for each region. For more information, see Best practices for Cassandra drivers.

  4. Set the following environment variables:

Driver authentication methods

There are two driver authentication methods: token authentication, or clientId and secret authentication.

  • Token authentication

  • Client ID and secret authentication

This authentication method is supported and recommended for most recent driver versions.

In your driver authentication code, pass the literal string token as the username and your application token value (AstraCS:…​) as the password. For example:

("token", "AstraCS:...")

If you are on an older driver version that doesn’t support token authentication, then you might need to use clientId and secret.

When you generate an application token, download or copy the token.json that contains the following values:

{
  "clientId": "CLIENT_ID",
  "secret": "CLIENT_SECRET",
  "token": "APPLICATION_TOKEN"
}

In your driver authentication code, pass clientId as the username and secret as the password. For example:

("CLIENT_ID", "SECRET")

For more information, see Token details.

Install the C# driver

  1. Create a new C# project:

    mkdir csharpproject
    cd csharpproject
    dotnet new console
  2. Add a dependency for the C# driver Latest CassandraCSharpDriver release on NuGet:

    dotnet add package CassandraCSharpDriver -v VERSION

    If you choose to install an earlier version, make sure you choose a version that is compatible with Astra DB. For more information, see Cassandra drivers supported by DataStax.

Connect the C# driver

  1. Replace the code in Program.cs with the following connection code, replacing PATH_TO_SCB with the absolute path to your database’s Secure Connect Bundle (SCB) (secure-connect-DATABASE_NAME.zip):

    Program.cs
    using System;
    using System.Linq;
    using Cassandra;
    
    namespace csharpproject
    {
        class Program
        {
            static void Main(string[] args)
            {
                var session =
                    Cluster.Builder()
                           .WithCloudSecureConnectionBundle(@"C:PATH_TO_SCB")
                           .WithCredentials("token", "ASTRA_DB_APPLICATION_TOKEN")
                           .Build()
                           .Connect();
            }
        }
    }
  2. After the connection code, add code to the Main method in Program.cs that runs a CQL query and prints the output to the console:

    Program.cs
    var rowSet = session.Execute("select * from system.local");
    Console.WriteLine(rowSet.First().GetValue<string>("cluster_name"));
  3. Save Program.cs, and then run your C# project with the dotnet runtime:

    dotnet restore
    dotnet build
    dotnet run --no-build
  4. Extend or modify this script to run other commands against your database or connect to other databases. For more information, see C# driver documentation and DataStax-compatible Cassandra drivers.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax, an IBM Company | Privacy policy | Terms of use | Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com