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.
1. using System;
2. using System.Linq;
3. using Cassandra;
5. namespace Yugabyte_CSharp_Demo
6. {
7. class Program
8. {
9. static void Main(string[] args)
10. {
11. try
12. {
13. var cluster = Cluster.Builder()
14. .AddContactPoints("127.0.0.1")
15. .WithPort(9042)
16. .Build();
17. var session = cluster.Connect();
18. session.Execute("CREATE KEYSPACE IF NOT EXISTS ybdemo");
19. Console.WriteLine("Created keyspace ybdemo");
21. var createStmt = "CREATE TABLE IF NOT EXISTS ybdemo.employee(" +
22. "id int PRIMARY KEY, name varchar, age int, language varchar)";
23. session.Execute(createStmt);
24. Console.WriteLine("Created keyspace employee");
26. var insertStmt = "INSERT INTO ybdemo.employee(id, name, age, language) " +
27. "VALUES (1, 'John', 35, 'C#')";
28. session.Execute(insertStmt);
29. Console.WriteLine("Inserted data: {0}", insertStmt);
31. var preparedStmt = session.Prepare("SELECT name, age, language " +
32. "FROM ybdemo.employee WHERE id = ?");
33. var selectStmt = preparedStmt.Bind(1);
34. var result = session.Execute(selectStmt);
35. var rows = result.GetRows().ToList();
36. Console.WriteLine("Select query returned {0} rows", rows.Count());
37. Console.WriteLine("Name\tAge\tLanguage");
38. foreach (Row row in rows)
39. Console.WriteLine("{0}\t{1}\t{2}", row["name"], row["age"], row["language"]);
41. session.Dispose();
42. cluster.Dispose();
44. }
45. catch (Cassandra.NoHostAvailableException)
46. {
47. Console.WriteLine("Make sure YugabyteDB is running locally!.");
48. }
49. catch (Cassandra.InvalidQueryException ie)
50. {
51. Console.WriteLine("Invalid Query: " + ie.Message);
52. }
53. }
54. }
55. }
Running the C# app
Run the C# app from menu select Run -> Start Without Debugging
You should see the following as the output.
1. Created keyspace ybdemo
2. Created keyspace employee
3. Inserted data: INSERT INTO ybdemo.employee(id, name, age, language) VALUES (1, 'John', 35, 'C#')
4. Select query returned 1 rows
5. Name Age Language
6. 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.
1. using System;
2. using System.Collections.Generic;
3. using StackExchange.Redis;
5. namespace Yugabyte_CSharp_Demo
6. {
7. class Program
8. {
9. static private void printHash(HashEntry[] hashes)
10. {
11. foreach (var hashEntry in hashes)
12. {
13. Console.WriteLine(string.Format("{0}: {1}", hashEntry.Name, hashEntry.Value));
14. }
15. }
16. static void Main(string[] args)
17. {
18. try
19. {
20. ConfigurationOptions config = new ConfigurationOptions
21. {
22. EndPoints =
23. {
24. { "127.0.0.1", 6379 },
25. },
26. CommandMap = CommandMap.Create(new HashSet<string>
27. { // EXCLUDE commands that are not fully supported on Yugabyte side.
28. "SUBSCRIBE", "CLUSTER", "TIME", "PING"
29. }, available: false)
30. };
32. ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(config);
33. IDatabase redisDB = connection.GetDatabase();
34. var hashKey = "1";
35. HashEntry[] setHash = {
36. new HashEntry("name", "John"),
37. new HashEntry("age", 35),
38. new HashEntry("language", "C#"),
39. new HashEntry("client", "Redis")
40. };
41. Console.WriteLine("Successfully executed HMSET:");
42. printHash(setHash);
43. redisDB.HashSet(hashKey, setHash);
45. var getHash = redisDB.HashGetAll(hashKey);
46. Console.WriteLine("Successfully executed HMGET:");
47. printHash(getHash);
48. }
49. catch (RedisConnectionException e)
50. {
51. Console.WriteLine("Unable to make a connection to local YugabyteDB. " +
52. "Error:", e.Message);
53. }
54. }
55. }
56. }
Running the C# app
Run the C# app from menu select Run -> Start Without Debugging
You should see the following as the output.
1. Successfully executed HMSET:
2. name: John
3. age: 35
4. language: C#
5. client: Redis
6. Successfully executed HMGET:
7. age: 35
8. client: Redis
9. language: C#
10. name: John
