Develop C# Apps
Prerequisites
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 YugabyteDB 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
当前内容版权归 YugabyteDB 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 YugabyteDB .