Connect the C# driver to your database
This driver supports the vector and non-vector data types. However, DataStax recommends the Data API and clients for Serverless (Vector) databases. DataStax recommends CQL drivers only for Serverless (Non-Vector) databases, existing applications that previously used a CQL-based driver, or if you plan to exclusively use CQL. |
This example creates a new C# project and configures it to connect to your Serverless (Non-Vector) database. It adds dependencies, connection code, and code to run a CQL query and print the output to the console.
Prerequisites
-
Download your database’s Secure Connect Bundle (SCB).
Connect 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. The latest version is .
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
For installation and data migration instructions, see the C# driver quickstart.