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.
If possible, upgrade to the latest native driver. |
For more details about supported software, see the DataStax Support Policy.
Upgrade your driver to a compatible version to connect to DataStax Astra DB databases. For more information, see the DataStax Driver Matrix. |
Prerequisites
-
Create your database and set the environment variables for database ID, region, keyspace, and token. For more information, see Create your database.
-
Download and install the current version of the Microsoft .NET Core SDK.
-
Client ID and Client Secret by creating your application token for your username and password.
Working with secure connect bundle
This page explains how to use the secure connect bundle for Astra DB Classic. If you want to use the secure connect bundle with Astra DB Serverless, see the Astra DB Serverless documentation. |
Downloading secure connect bundle
To get the necessary security credentials and certificates for connecting drivers to your Astra DB database, you’ll need to download the secure connect bundle from the DataStax Astra Portal.
-
Open your Astra Portal and select your database.
-
On the Overview page, select Connect.
-
In the Database Essentials section, click Get Bundle.
-
In the Secure Connect Bundle Download dialog box, use the Select a region dropdown menu to select the region for which you want to download the bundle. If you have multiple regions, you can download a bundle for each region.
If you’ve enabled VPC peering, you can also Download External Secure Connect Bundle for use within your VPC peering. VPC peering is only available on Classic databases.
-
After selecting a region, various options for downloading the bundle appear. To download the bundle to your local computer, click Download Secure Bundle.
The secure connect bundle downloads as a ZIP file named
secure-connect-<database_name>.zip
.Expiration of download linkFor added security, the download link for the secure connect bundle expires after five minutes. Once the download link expires, you’ll need to repeat the steps above to regenerate a new download link.
Note that the secure connect bundle itself never expires.
Sharing secure connect bundle
Although teammates can access your Astra DB database, it doesn’t 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. |
Secure connect bundle contents
The Secure Connect Bundle (SCB) contains the following files:
File | Contents |
---|---|
|
DataStax’s Certificate Authority public certificate |
|
A certificate, unique to the specific SCB |
|
A private key, unique to the specific SCB |
|
A PFX formatted archive containing the certificate and the private key |
|
A configuration file with information on how to securely connect to the Astra DB instance associated with the SCB |
|
A CQLSH profile containing CQL shell session settings |
|
A Java keystore file containing the aforementioned cert & key files |
|
A Java keystore file containing the aforementioned ca.crt |
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. Latest
.
dotnet add package CassandraCSharpDriver -v <version>
-
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 theWithCloudSecureConnectionBundle
method call, and your credentials in theWithCredentials
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