|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- docs |
| 4 | +- develop |
| 5 | +- stack |
| 6 | +- oss |
| 7 | +- rs |
| 8 | +- rc |
| 9 | +- oss |
| 10 | +- kubernetes |
| 11 | +- clients |
| 12 | +description: Connect your .NET application to a Redis database |
| 13 | +linkTitle: C#/.NET |
| 14 | +title: C#/.NET guide |
| 15 | +weight: 2 |
| 16 | +--- |
| 17 | + |
| 18 | +[NRedisStack](https://github.com/redis/NRedisStack) is the .NET client for Redis. |
| 19 | +The sections below explain how to install `NRedisStack` and connect your application |
| 20 | +to a Redis database. |
| 21 | + |
| 22 | +`NRedisStack` requires a running Redis or [Redis Stack]({{< relref "/operate/oss_and_stack/install/install-stack/" >}}) server. See [Getting started]({{< relref "/operate/oss_and_stack/install/" >}}) for Redis installation instructions. |
| 23 | + |
| 24 | +You can also access Redis with an object-mapping client interface. See |
| 25 | +[Redis OM for .NET]({{< relref "/integrate/redisom-for-net" >}}) |
| 26 | +for more information. |
| 27 | + |
| 28 | +## Install |
| 29 | + |
| 30 | +Using the `dotnet` CLI, run: |
| 31 | + |
| 32 | +```bash |
| 33 | +dotnet add package NRedisStack |
| 34 | +``` |
| 35 | + |
| 36 | +## Connect and test |
| 37 | + |
| 38 | +Connect to localhost on port 6379. |
| 39 | + |
| 40 | +```csharp |
| 41 | +using NRedisStack; |
| 42 | +using NRedisStack.RedisStackCommands; |
| 43 | +using StackExchange.Redis; |
| 44 | +//... |
| 45 | +ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost"); |
| 46 | +IDatabase db = redis.GetDatabase(); |
| 47 | +``` |
| 48 | + |
| 49 | +You can test the connection by storing and retrieving a simple string. |
| 50 | + |
| 51 | +```csharp |
| 52 | +db.StringSet("foo", "bar"); |
| 53 | +Console.WriteLine(db.StringGet("foo")); // prints bar |
| 54 | +``` |
| 55 | + |
| 56 | +Store and retrieve a HashMap. |
| 57 | + |
| 58 | +```csharp |
| 59 | +var hash = new HashEntry[] { |
| 60 | + new HashEntry("name", "John"), |
| 61 | + new HashEntry("surname", "Smith"), |
| 62 | + new HashEntry("company", "Redis"), |
| 63 | + new HashEntry("age", "29"), |
| 64 | + }; |
| 65 | +db.HashSet("user-session:123", hash); |
| 66 | + |
| 67 | +var hashFields = db.HashGetAll("user-session:123"); |
| 68 | +Console.WriteLine(String.Join("; ", hashFields)); |
| 69 | +// Prints: |
| 70 | +// name: John; surname: Smith; company: Redis; age: 29 |
| 71 | +``` |
| 72 | +## Redis Stack modules |
| 73 | + |
| 74 | +To access Redis Stack capabilities, use the appropriate interface like this: |
| 75 | + |
| 76 | +``` |
| 77 | +IBloomCommands bf = db.BF(); |
| 78 | +ICuckooCommands cf = db.CF(); |
| 79 | +ICmsCommands cms = db.CMS(); |
| 80 | +IGraphCommands graph = db.GRAPH(); |
| 81 | +ITopKCommands topk = db.TOPK(); |
| 82 | +ITdigestCommands tdigest = db.TDIGEST(); |
| 83 | +ISearchCommands ft = db.FT(); |
| 84 | +IJsonCommands json = db.JSON(); |
| 85 | +ITimeSeriesCommands ts = db.TS(); |
| 86 | +``` |
| 87 | + |
| 88 | +## More information |
| 89 | + |
| 90 | +See the other pages in this section for more information and examples. |
0 commit comments