Connect the C# driver to your database
The C# driver supports the vector data type. These instructions are for users connecting to Hyper-Converged Database (HCD). |
This example creates a new C# project and configures it to connect to your HCD database. It adds dependencies, connection code, and code to run a CQL query and print the output to the console.
Prerequisite
-
You have installed a cluster.
-
You have created a keyspace.
-
You have installed C#.
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 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 HCD database.Include your credentials in the
WithCredentials
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() .WithCredentials("username", "password") .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
Installation and data migration instructions are located in the C# driver quickstart.