C# driver quickstart

DataStax recommends the Data API and clients for Serverless (Vector) databases. You can use the Data API to perform CQL operations on your table data in Serverless (Vector) databases.

DataStax recommends drivers only for Serverless (Non-Vector) databases, legacy applications that rely on a driver, or for CQL functions that aren’t supported by the Data API. For more information, see Compare connection methods.

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

This quickstart explains how to install the driver, connect it to your database, and, if necessary, migrate an existing DataStax C# driver to a version that supports Astra DB.

Prerequisites

  1. Create a database.

  2. Set the following environment variables:

    • ASTRA_DB_ID: The database ID

    • ASTRA_DB_REGION: A region where your database is deployed and where you want to connect to the database, such as us-east-2

    • ASTRA_DB_KEYSPACE: A keyspace in your database, such as default_keyspace

    • ASTRA_DB_APPLICATION_TOKEN: An application token with the Database Administrator role.

      The token.json has the following format:

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

      For driver authentication, you can use either clientId and secret or the literal string token and the AstraCS token value. If you are on an older driver version that doesn’t support the token option, then you might need to use clientId and secret. For more information, see Token details.

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

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

Install the C# driver

  1. Create a new C# project:

    shell
    mkdir csharpproject
    cd csharpproject
    dotnet new console
  2. Add the C# driver Latest CassandraCSharpDriver release on NuGet dependencies to your project:

    shell
    dotnet add package CassandraCSharpDriver -v VERSION

    Make sure you use a driver version that is compatible with Astra DB. For more information, see DataStax driver matrix.

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.cscs
    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.cscs
    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:

    shell
    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 Developing applications with DataStax drivers.

Migrate the C# driver

If necessary, you can migrate an earlier DataStax C# driver to a version that supports Astra DB.

  1. Complete the prerequisites.

  2. Add the C# driver Latest CassandraCSharpDriver release on NuGet dependencies to your project:

    shell
    dotnet add package CassandraCSharpDriver -v VERSION
  3. In your existing DataStax C# driver code, modify the connection code to use the SCB credentials. For more information, see Connect the C# driver.

    cs
                var session =
                    Cluster.Builder()
                           .WithCloudSecureConnectionBundle(@"C:PATH_TO_SCB")
                           .WithCredentials("clientId", "secret")
                           .Build()
                           .Connect();
  4. Run your application.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax | Privacy policy | Terms of use

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