C# driver quickstart

The DataStax C# driver does not support Serverless (Vector) databases. These instructions are for Serverless (Non-Vector) databases.

Use the C# driver only if your application previously used a CQL-based driver or if you need to explicitly use CQL. For more information, see Connection methods comparison.

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 can connect to your Serverless (Non-Vector) database.

Prerequisites

Connect the C# driver to your database

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

    mkdir csharpproject
    cd csharpproject
    dotnet new console
  2. Add the C# driver dependencies to your project. The latest version is Nuget.

    dotnet add package CassandraCSharpDriver -v <version>
  3. Replace the code in Program.cs with the following code to connect to your Serverless (Non-Vector) database.

    In the WithCloudSecureConnectionBundle method call, include the absolute path to the secure connect bundle for your Serverless (Non-Vector) database (secure-connect-DATABASE_NAME.zip). In the WithCredentials method call, include your credentials.

    using System;
    using System.Linq;
    using Cassandra;
    
    namespace csharpproject
    {
        class Program
        {
            static void Main(string[] args)
            {
                var session =
                    Cluster.Builder()
                           .WithCloudSecureConnectionBundle(@"/SECURE_CONNECT_BUNDLE_PATH/secure-connect-DATABASE_NAME.zip")
                           .WithCredentials("clientId", "clientSecret")
                           .Build()
                           .Connect();
            }
        }
    }
  4. 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"));
  5. Run your C# project with the dotnet runtime:

    dotnet restore
    dotnet build
    dotnet run --no-build

Migrate the C# driver

If necessary, you can migrate an earlier DataStax C# driver to a version that can connect to your Serverless (Non-Vector) database.

  1. Complete the prerequisites.

  2. Add the C# driver dependencies to your project. The latest version is Nuget.

    dotnet add package CassandraCSharpDriver -v <version>
  3. In your existing DataStax C# driver code, modify the connection code to use the secure connect bundle.

    In the WithCloudSecureConnectionBundle method call, include the path to the secure connect bundle for your Astra DB database (secure-connect-DATABASE_NAME.zip). In the WithCredentials method call, include your credentials.

    var session =
      Cluster.Builder()
          .WithCloudSecureConnectionBundle(@"/SECURE_CONNECT_BUNDLE_PATH/secure-connect-DATABASE_NAME.zip")
            .WithCredentials("clientId", "clientSecret")
            .Build()
            .Connect();
  4. Run your application.

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