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
-
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 DB console 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 Connect using a driver section, click your preferred language from the list to load language-specific instructions.
If you have multiple regions, select the region you want to connect to from the dropdown menu for instructions.
Note, though, that 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.
If you’ve enabled VPC peering, you can also Download External Secure Connect Bundle for use within your VPC peering. The secure-connect-database_name.zip file downloads, which contains the security certificates and credentials for your database.
VPC peering is only available on Classic databases. |
Alternatively, you can right-click the Download credentials link, copy the link source, and then use a
|
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 the Astra DB console.
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.15.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 (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\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.