Develop C# Apps
AttentionThis page documents an earlier version. Go to the latest (v2.1)version.
Pre-requisites
This tutorial assumes that you have:
- installed YugabyteDB, created a universe and are able to interact with it using the CQL shell. If not, please follow these steps in the quick start guide.
- installed Visual Studio
Writing a HelloWorld C# app
In your Visual Studio create a new Project and choose Console Application as template. Follow the instructions to save the project.
Install Cassandra C# driver
To install the driver in your Visual Studio project
- Open your Project Solution View.
- Right-click on Packages and click Add Packages.
- Search for CassandraCSharpDriver and click Add Package.
Copy the contents below to your Program.cs file.
using System;
using System.Linq;
using Cassandra;
namespace Yugabyte_CSharp_Demo
{
class Program
{
static void Main(string[] args)
{
try
{
var cluster = Cluster.Builder()
.AddContactPoints("127.0.0.1")
.WithPort(9042)
.Build();
var session = cluster.Connect();
session.Execute("CREATE KEYSPACE IF NOT EXISTS ybdemo");
Console.WriteLine("Created keyspace ybdemo");
var createStmt = "CREATE TABLE IF NOT EXISTS ybdemo.employee(" +
"id int PRIMARY KEY, name varchar, age int, language varchar)";
session.Execute(createStmt);
Console.WriteLine("Created keyspace employee");
var insertStmt = "INSERT INTO ybdemo.employee(id, name, age, language) " +
"VALUES (1, 'John', 35, 'C#')";
session.Execute(insertStmt);
Console.WriteLine("Inserted data: {0}", insertStmt);
var preparedStmt = session.Prepare("SELECT name, age, language " +
"FROM ybdemo.employee WHERE id = ?");
var selectStmt = preparedStmt.Bind(1);
var result = session.Execute(selectStmt);
var rows = result.GetRows().ToList();
Console.WriteLine("Select query returned {0} rows", rows.Count());
Console.WriteLine("Name\tAge\tLanguage");
foreach (Row row in rows)
Console.WriteLine("{0}\t{1}\t{2}", row["name"], row["age"], row["language"]);
session.Dispose();
cluster.Dispose();
}
catch (Cassandra.NoHostAvailableException)
{
Console.WriteLine("Make sure YugabyteDB is running locally!.");
}
catch (Cassandra.InvalidQueryException ie)
{
Console.WriteLine("Invalid Query: " + ie.Message);
}
}
}
}
Running the C# app
Run the C# app from menu select Run -> Start Without Debugging
You should see the following as the output.
Created keyspace ybdemo
Created keyspace employee
Inserted data: INSERT INTO ybdemo.employee(id, name, age, language) VALUES (1, 'John', 35, 'C#')
Select query returned 1 rows
Name Age Language
John 35 C#
Pre-requisites
This tutorial assumes that you have:
- installed YugabyteDB, created a universe and are able to interact with it using the Redis shell. If not, please follow these steps in the quick start guide.
- installed Visual Studio
Writing a HelloWorld C# app
In your Visual Studio create a new Project and choose Console Application as template. Follow the instructions to save the project.
Install StackExchange.Redis C# driver
To install the driver in your Visual Studio project
- Open your Project Solution View.
- Right-click on Packages and click Add Packages.
- Search for StackExchange.Redis and click Add Package.
Copy the contents below to your Program.cs file.
using System;
using System.Collections.Generic;
using StackExchange.Redis;
namespace Yugabyte_CSharp_Demo
{
class Program
{
static private void printHash(HashEntry[] hashes)
{
foreach (var hashEntry in hashes)
{
Console.WriteLine(string.Format("{0}: {1}", hashEntry.Name, hashEntry.Value));
}
}
static void Main(string[] args)
{
try
{
ConfigurationOptions config = new ConfigurationOptions
{
EndPoints =
{
{ "127.0.0.1", 6379 },
},
CommandMap = CommandMap.Create(new HashSet<string>
{ // EXCLUDE commands that are not fully supported on Yugabyte side.
"SUBSCRIBE", "CLUSTER", "TIME", "PING"
}, available: false)
};
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(config);
IDatabase redisDB = connection.GetDatabase();
var hashKey = "1";
HashEntry[] setHash = {
new HashEntry("name", "John"),
new HashEntry("age", 35),
new HashEntry("language", "C#"),
new HashEntry("client", "Redis")
};
Console.WriteLine("Successfully executed HMSET:");
printHash(setHash);
redisDB.HashSet(hashKey, setHash);
var getHash = redisDB.HashGetAll(hashKey);
Console.WriteLine("Successfully executed HMGET:");
printHash(getHash);
}
catch (RedisConnectionException e)
{
Console.WriteLine("Unable to make a connection to local YugabyteDB. " +
"Error:", e.Message);
}
}
}
}
Running the C# app
Run the C# app from menu select Run -> Start Without Debugging
You should see the following as the output.
Successfully executed HMSET:
name: John
age: 35
language: C#
client: Redis
Successfully executed HMGET:
age: 35
client: Redis
language: C#
name: John