How to connect C# and Cassandra with

Viewed 2642

I am new to .NET C# and Cassandra and I am not able to connect them with each other. I have searched a lot and haven't found a clear explaining on how it works.

I have downloaded Cassandra installed it with Python 2.7, I can run the server and I can run cqlsh. Then I open Visual studio, create a new .NET Core project and install package of Cassandra C# driver.

That's all, I don't know how to create a table, key-spaces from C# to Cassandra.

Can anyone give a simple explanation on how can I create a simple key-space, tables with columns to output the code, so that I can see how it works?

1 Answers

It's five years old, but I wrote-up an article on how to use Cassandra as a backend for a ASP.NET MVC project: http://www.aaronstechcenter.com/aspnet_mvc_cassandra.php

My Git repo for the article is still out there, too: https://github.com/aploetz/ShipCrew

The meat of it will be in the CassandraDAO.cs:

private Cluster Connect() {
    string user = getAppSetting("cassandraUser");
    string pwd = getAppSetting("cassandraPassword");
    string[] nodes = getAppSetting("cassandraNodes").Split(',');

    QueryOptions queryOptions = new QueryOptions().SetConsistencyLevel(ConsistencyLevel.One);


    Cluster cluster = Cluster.Builder()
        .AddContactPoints(nodes)
        .WithCredentials(user, pwd)
        .WithQueryOptions(queryOptions)
        .Build();

    return cluster;
}

I'm sure the driver versions are way out-of-date, but it should be enough to get you started.

Related