C# driver quickstart
This driver supports the vector and non-vector data types. DataStax recommends the Data API and clients for Serverless (Vector) databases. You can use the Data API to perform CQL operations on your table data in Serverless (Vector) databases. DataStax recommends drivers only for Serverless (Non-Vector) databases, existing applications that previously used a CQL-based driver, or if you need to use some CQL functions that aren’t supported by the Data API. For more information, see Connection methods comparison. |
This quickstart explains how to install the driver, connect it to your database, and, if necessary, migrate an existing DataStax C# driver to a version that can connect to your Serverless (Non-Vector) database.
Prerequisites
-
An active Astra account
-
An active Serverless (Non-Vector) database
-
The latest .NET SDK downloaded and installed
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 C# driver dependencies to your project.
dotnet add package CassandraCSharpDriver -v <version>
-
Replace the code in
Program.cs
with the following code to connect to your Serverless (Non-Vector) database.In the
WithCloudSecureConnectionBundle
method call, include the absolute path to your Serverless (Non-Vector) database’s SCB file (secure-connect-DATABASE_NAME.zip
). In theWithCredentials
method call, include your credentials.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
If necessary, you can migrate an earlier DataStax C# driver to a version that can connect to your Serverless (Non-Vector) database.
-
Add the C# driver dependencies to your project.
dotnet add package CassandraCSharpDriver -v <version>
-
In your existing DataStax C# driver code, modify the connection code to use the SCB.
In the
WithCloudSecureConnectionBundle
method call, include the path to the SCB for your Astra DB database (secure-connect-DATABASE_NAME.zip
). In theWithCredentials
method call, include your credentials.var session = Cluster.Builder() .WithCloudSecureConnectionBundle(@"/SECURE_CONNECT_BUNDLE_PATH/secure-connect-DATABASE_NAME.zip") .WithCredentials("clientId", "clientSecret") .Build() .Connect();
-
Run your application.