Connect with the C# driver
|
DataStax recommends the Data API and clients for Serverless (vector) databases. You can use the Data API to run CQL statements on tables in Serverless (vector) databases. DataStax recommends drivers only for Serverless (non-vector) databases, legacy applications that rely on a driver, or CQL functions that aren’t supported by the Data API. For more information, see Connect to Astra DB Serverless databases. |
Because Astra DB is based on Apache Cassandra®, you can use Cassandra drivers to connect to your Astra DB Serverless databases.
To use the C# driver, you need to install the driver and its dependencies, and then connect the driver to your database. Once connected, you can write scripts that use the driver to run commands against your database.
This quickstart explains how to install the driver and connect it to your database. It also explains how to upgrade from an earlier version of the C# driver to a version that supports Astra DB.
Prerequisites
-
Download and install the current version of the Microsoft .NET Core SDK.
-
Download your database’s Secure Connect Bundle (SCB).
For multi-region databases, download the SCB for a region that is geographically close to your application to reduce latency.
If you need to connect to multiple regions in the same application, you need the SCB for each region, and your driver code must instantiate one root object (
session) for each region. For more information, see Best practices for Cassandra drivers. -
Set the following environment variables:
-
DATABASE_ID: The database ID. -
APPLICATION_TOKEN: An application token with the Database Administrator role.
-
Driver authentication methods
There are two driver authentication methods:
tokenauthentication-
The
tokenauthentication method is supported and recommended for most recent driver versions.In your driver authentication code, pass the literal string
tokenas the username and your application token value (AstraCS:…) as the password. For example:("token", "AstraCS:...") clientIdandsecretauthentication-
If you are on an older driver version that doesn’t support
tokenauthentication, then you might need to useclientIdandsecret.When you generate an application token, download or copy the
token.jsonthat contains the following values:{ "clientId": "CLIENT_ID", "secret": "CLIENT_SECRET", "token": "APPLICATION_TOKEN" }Then, in your driver authentication code, pass
clientIdas the username andsecretas the password. For example:("CLIENT_ID", "SECRET")
For more information, see Token details.
Install the C# driver
-
Create a new C# project:
mkdir csharpproject cd csharpproject dotnet new console -
Add a dependency for the C# driver
:
dotnet add package CassandraCSharpDriver -v VERSIONIf you install an earlier version of the driver, make sure your version is compatible with Astra DB. If you need to query vector data in Astra DB Serverless (vector) databases, make sure your version also supports vector data. For more information, see Cassandra drivers supported by DataStax.
Connect the C# driver
-
Replace the code in
Program.cswith the following connection code, and then replacePATH\TO\SCB.zipwith the absolute path to your database’s Secure Connect Bundle (SCB) zip file (secure-connect-DATABASE_NAME.zip):Program.csusing System; using System.Linq; using Cassandra; namespace csharpproject { class Program { static void Main(string[] args) { var session = Cluster.Builder() .WithCloudSecureConnectionBundle(@"C:PATH\TO\SCB.zip") .WithCredentials("token", "APPLICATION_TOKEN") .Build() .Connect(); } } } -
After the connection code, add code to the
Mainmethod inProgram.csthat runs a CQL query and prints the output to the console.The following example queries the
system.localtable. You can replace the exampleSELECTstatement with any CQL statement that you want to run against a keyspace and table in your database.Program.csvar rowSet = session.Execute("select * from system.local"); Console.WriteLine(rowSet.First().GetValue<string>("cluster_name")); -
Save
Program.cs, and then run your C# project with thedotnetruntime:dotnet restore dotnet build dotnet run --no-build -
Extend or modify this script to run other commands against your database or connect to other databases. For more information, see C# driver documentation and DataStax-compatible Cassandra drivers.
Upgrade the C# driver
Use these steps if you need to upgrade from an earlier version of the C# driver to a version that supports Astra DB:
-
Add a dependency for the C# driver
:
dotnet add package CassandraCSharpDriver -v VERSION -
In your existing C# driver code, modify the connection code to use the SCB and
tokenauthentication:var session = Cluster.Builder() .WithCloudSecureConnectionBundle(@"C:PATH\TO\SCB.zip") .WithCredentials("token", "APPLICATION_TOKEN") .Build() .Connect();For more information, see Connect the C# driver.
-
Run your application.