C# driver quickstart
The C# driver does not support the Serverless (Vector) type. These instructions are for users connecting to a Serverless (Non-Vector) database. |
Use the C# driver only if your application previously used a CQL-based driver or if you need to explicitly use CQL.
Review the Connection methods comparison page to determine the best option for your use case.
This quickstart provides an end-to-end workflow for how to install the driver, connect it to your database, and migrate an existing DataStax C# driver to a version that is capable of connecting to your Serverless (Non-Vector) database.
Prerequisites
You need the following items to complete this quickstart:
-
An active Astra account.
-
A Serverless (Non-Vector) database created in the Astra Portal.
-
A secure connect bundle downloaded from the Astra Portal.
-
The latest .NET SDK downloaded and installed. A five-minute Microsoft hello world tutorial for VS Code users is also available.
Connect the C# driver to your database
-
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 Serverless (Non-Vector) database.Include the absolute path to the secure connect bundle for your Serverless (Non-Vector) 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(@"/SECURE_CONNECT_BUNDLE_PATH/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
Migrate the C# driver
Complete the following procedure to migrate your existing DataStax C# driver to a version that is capable of connecting to your Serverless (Non-Vector) database.
-
Add the dependencies for the C# driver to your project. Latest .
dotnet add package CassandraCSharpDriver -v <version>
-
In your existing DataStax C# driver code, modify the connection code to use the Serverless (Non-Vector) 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(@"/SECURE_CONNECT_BUNDLE_PATH/secure-connect-DATABASE_NAME.zip") .WithCredentials("clientId", "clientSecret") .Build() .Connect();
-
Run your application.