C# driver quickstart
This quickstart creates a new C# project with the C# driver connected to a DSE database. It adds dependencies, connection code, and code to run a Cassandra Query Language (CQL) query and print the output to the console.
Prerequisites
-
A running DSE cluster
-
The latest .NET SDK installed
Connect the C# driver to your database
-
Create a new C# project:
mkdir csharpproject cd csharpproject dotnet new console -
Add a dependency for the C# driver
:
dotnet add package CassandraCSharpDriver -v <version>The C# driver version 3.21 and later supports the vector data type.
-
Replace the code in
Program.cswith the following code to connect to your DSE database.Include your credentials in the
WithCredentialsmethod 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() .WithCredentials("username", "password") .Build() .Connect(); } } } -
After the connection code, add the following code to the
Mainmethod 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
dotnetruntime:dotnet restore dotnet build dotnet run --no-build