DataStax C# driver
Before using the DataStax driver, review the Best practices for DataStax drivers to understand the rules and recommendations for improving performance and minimizing resource utilization in applications that use a DataStax driver.
DataStax recommends using the unified DataStax driver. See this blog post for more information. You can also use the DataStax Enterprise (DSE) drivers, which exposes the same API for connecting to Cassandra databases. |
Prerequisites
-
Create your database and set your environment variables to save time developing on your database. There are four environment variables, three of which are on the Astra dashboard (database id, region, and keyspace), and one that you must create (token). For more, see Create your database.
-
Download and install the current version of the .NET Core SDK.
-
Client ID and Client Secret by creating your application token for your username and password.
Working with secure connect bundle
Downloading secure connect bundle
To connect to your Astra DB database using the drivers, download the secure database bundle from the DataStax Astra Portal that contains the connection credentials.
-
Open a browser, navigate to Astra DB, and log in.
-
From your Dashboard page, select your database.
-
On the Overview page, select Connect.
-
In the Select a Method section, select Drivers and then Legacy from the dropdown menu to select your driver type from the list to load language-specific instructions.
Java and Node.js are available as legacy and cloud native drivers.
If you have multiple regions, select the region you want to connect to from the dropdown menu for instructions.
The bundle URL is the same for all languages.
-
Click Download Bundle.
If you have multiple regions, you will have the option to download a bundle for each region from the expanded Download Bundle drop-down menu.
The secure-connect-database_name.zip file downloads, which contains the security certificates and credentials for your database.
Sharing secure connect bundle
Although teammates can access your Astra DB database, it will not display in their list of available databases under My Databases in Astra Portal.
After you create an Astra DB database, you can grant access to other members of your team by providing them with the database credentials and connection details for your database.
Be careful when sharing connection details. Providing this information to another user grants them access to your Astra DB database and ownership capabilities, such as making modifications to the database. For security, delete downloaded connection credentials after sending them to your teammate. |
Connecting C# driver
-
Create a new C# project and configure it to connect to your Cassandra database.
mkdir csharpproject
cd csharpproject
dotnet new console
-
Add the dependencies for the C# driver to your project.
dotnet add package CassandraCSharpDriver -v 3.17.0
-
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 for your Astra DB database ( |
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("clientId", "clientSecret")
.Build()
.Connect();
}
}
}
-
After the connection code, add the following code to the
Main
method inProgram.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"));
-
Run your C# project with the
dotnet
runtime.
dotnet restore
dotnet build
dotnet run --no-build
Migrating C# driver
Complete the following procedure to migrate your existing DataStax C# driver to a version that is capable of connecting to Astra databases created using DataStax Astra DB.
-
Add the dependencies for the C# driver to your project.
-
In your existing DataStax C# driver code, modify the connection code to use the Astra DB API.
Include the absolute path to the secure connect bundle for your Cassandra database (
secure-connect-database_name.zip
) in theWithCloudSecureConnectionBundle
method call, and your credentials in theWithCredentials
method call, as shown in the following examples.var session = Cluster.Builder() .WithCloudSecureConnectionBundle(@"C:\path\to\secure-connect-database_name.zip") .WithCredentials("clientId", "clientSecret") .Build() .Connect();
var session = DseCluster.Builder() .WithCloudSecureConnectionBundle(@"C:\path\to\secure-connect-database_name.zip") .WithCredentials("clientId", "clientSecret") .Build() .Connect();
-
Run your application.