Connecting to your database with the C# driver

Use the DataStax C# driver to connect to your database created using the DataStax Constellation console.

Use the DataStax C# driver to connect to your Apollo database and begin building your own application.
Tip: DataStax recommends using the DataStax C# driver for Apache Cassandra. You can also use the DataStax Enterprise (DSE) C# driver, which exposes the same API for connecting to Apollo databases.

Prerequisites

  1. Download and install the current version of the .NET Core SDK.
  2. Create a DataStax Apollo database.
  3. Download the secure connect bundle to obtain connection credentials for your database.

Procedure

  1. Create a new C# project and configure it to connect to your Cassandra database.
    mkdir csharpproject
    cd csharpproject
    dotnet new console
    1. Add the dependencies for the C# driver to your project.
      • DataStax C# driver for Apache Cassandra

        dotnet add package CassandraCSharpDriver -v 3.12.0
      • DSE C# driver

        dotnet add package Dse -v 2.9.0
    2. Replace the code in Program.cs with the following code to connect to your Apollo database.
      Note: Include the absolute path to the secure connect bundle for your Apollo 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.
      • DataStax C# driver for Apache Cassandra

        using System;
        using System.Linq;
        using Cassandra;
        
        namespace csharpproject
        {
            class Program
            {
                static void Main(string[] args)
                {
                    var session = 
                        Cluster.Builder()
                               .WithCloudSecureConnectionBundle(@"C:\path\to\secure-connect-database_name.zip")
                               .WithCredentials("username", "password")
                               .Build()
                               .Connect();               
                }
            }
        }
      • DSE C# driver

        using System;
        using System.Linq;
        using Dse;
        
        namespace csharpproject
        {
            class Program
            {
                static void Main(string[] args)
                {
                    var session = 
                        DseCluster.Builder()
                               .WithCloudSecureConnectionBundle(@"C:\path\to\secure-connect-database_name.zip")
                               .WithCredentials("username", "password")
                               .Build()
                               .Connect();               
                }
            }
        }
    3. 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"));
  2. Run your C# project with the dotnet runtime.
    dotnet restore
    dotnet build
    dotnet run --no-build

What's next

Build your application. See the DataStax C# driver for Apache Cassandra and DSE C# driver documentation for more information about using the DataStax C# driver.