C# driver quickstart

This quickstart creates a new C# project with the C# driver connected to a DSE database. It adds dependencies, connection code, and code to run a Cassandra Query Language (CQL) query and print the output to the console.

Prerequisites

Connect the C# driver to your database

  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>

    The C# driver version 3.21 and later supports the vector data type.

  3. Replace the code in Program.cs with the following code to connect to your DSE database.

    Include 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()
                           .WithCredentials("username", "password")
                           .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

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2025 | 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: Contact IBM