DataStax C# driver

Before using a DataStax driver, review Best practices for DataStax drivers and upgrade or install the latest supported driver for your language. For more details about supported software, see the DataStax Support Policy.

Upgrade your driver to a compatible version to connect to Astra DB databases. For more information, see the DataStax Driver Matrix.

Connect the C# driver

  1. Create a database, and then set environment variables for database ID, region, and keyspace.

  2. Create an application token, and then set a token environment variable.

    The token.json has the following format:

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

    For authentication with the driver, 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 token and AstraCS, then you might need to use clientId and secret.

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

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

  5. Create a new C# project and configure it to connect to your Astra DB database:

    mkdir csharpproject
    cd csharpproject
    dotnet new console
  6. Add the dependencies for the latest C# driver Nuget to your project:

    dotnet add package CassandraCSharpDriver -v <version>
  7. 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 (SCB) 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_SCB")
                           .WithCredentials("clientId", "clientSecret")
                           .Build()
                           .Connect();
            }
        }
    }
  8. 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"));
  9. Run your C# project with the dotnet runtime:

    dotnet restore
    dotnet build
    dotnet run --no-build

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 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