|
| 1 | +:::{include} /_include/links.md |
| 2 | +::: |
| 3 | + |
| 4 | +(connect-csharp)= |
| 5 | +# C# |
| 6 | + |
| 7 | +:::{div} sd-text-muted |
| 8 | +Connect to CrateDB from C# .NET applications. |
| 9 | +::: |
| 10 | + |
| 11 | +:::{rubric} About |
| 12 | +::: |
| 13 | + |
| 14 | +[Npgsql] is an open source ADO\.NET Data Provider for PostgreSQL, for programs |
| 15 | +written in C#, F#, or Visual Basic. |
| 16 | + |
| 17 | +:::{rubric} Synopsis |
| 18 | +::: |
| 19 | + |
| 20 | +`example.csproj` |
| 21 | +```xml |
| 22 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 23 | + |
| 24 | + <PropertyGroup> |
| 25 | + <OutputType>Exe</OutputType> |
| 26 | + <TargetFramework>net$(NETCoreAppMaximumVersion)</TargetFramework> |
| 27 | + <GenerateProgramFile>false</GenerateProgramFile> |
| 28 | + </PropertyGroup> |
| 29 | + |
| 30 | + <ItemGroup> |
| 31 | + <PackageReference Include="Npgsql" Version="10.0.0-rc.1" /> |
| 32 | + </ItemGroup> |
| 33 | + |
| 34 | +</Project> |
| 35 | +``` |
| 36 | +`example.cs` |
| 37 | +```c# |
| 38 | +using Npgsql; |
| 39 | +using System; |
| 40 | + |
| 41 | +// Connect to database. |
| 42 | +var connString = "Host=localhost;Port=5432;Username=crate;Password=crate;Database=doc;Sslmode=disable"; |
| 43 | +var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString); |
| 44 | +var dataSource = dataSourceBuilder.Build(); |
| 45 | +var conn = dataSource.OpenConnection(); |
| 46 | + |
| 47 | +// Invoke basic query. |
| 48 | +var cmd = new NpgsqlCommand("SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3", conn); |
| 49 | + |
| 50 | +// Display results. |
| 51 | +var reader = cmd.ExecuteReader(); |
| 52 | +while (reader.Read()) |
| 53 | + Console.WriteLine($"{reader.GetString(0)}: {reader.GetInt32(1)}"); |
| 54 | +``` |
| 55 | + |
| 56 | +Start CrateDB using Docker or Podman, then invoke the example program. |
| 57 | +```shell |
| 58 | +docker run --rm --publish=5432:5432 crate '-Cdiscovery.type=single-node' |
| 59 | +``` |
| 60 | +```shell |
| 61 | +export DOTNET_CLI_TELEMETRY_OPTOUT=true |
| 62 | +dotnet run |
| 63 | +``` |
| 64 | + |
| 65 | +:::{rubric} CrateDB Cloud |
| 66 | +::: |
| 67 | + |
| 68 | +For connecting to CrateDB Cloud, use `Sslmode=require`, and |
| 69 | +replace hostname, username, and password with values matching |
| 70 | +your environment. |
| 71 | +```c# |
| 72 | +var connString = "Host=testcluster.cratedb.net;Port=5432;Username=admin;Password=password;Database=doc;Sslmode=require"; |
| 73 | +``` |
| 74 | + |
| 75 | +## Examples |
| 76 | + |
| 77 | +:::{card} |
| 78 | +:link: https://github.com/crate/cratedb-examples/tree/main/by-language/csharp-npgsql |
| 79 | +:link-type: url |
| 80 | +{material-outlined}`play_arrow;2em` |
| 81 | +Connect to CrateDB and CrateDB Cloud using .NET (C#) |
| 82 | ++++ |
| 83 | +Demonstrates a basic example using Npgsql with CrateDB. |
| 84 | +::: |
| 85 | + |
| 86 | +:::{card} |
| 87 | +:link: https://github.com/crate/cratedb-examples/tree/main/by-language/csharp-efcore |
| 88 | +:link-type: url |
| 89 | +{material-outlined}`play_arrow;2em` |
| 90 | +Connect to CrateDB and CrateDB Cloud using the Npgsql Entity Framework |
| 91 | ++++ |
| 92 | +Demonstrates the Npgsql Entity Framework Core provider for PostgreSQL with CrateDB. |
| 93 | +::: |
| 94 | + |
| 95 | +[](https://github.com/crate/cratedb-examples/actions/workflows/lang-csharp-npgsql.yml) |
| 96 | +[](https://github.com/crate/cratedb-examples/actions/workflows/lang-csharp-efcore.yml) |
| 97 | + |
| 98 | +## See also |
| 99 | + |
| 100 | +:::{div} |
| 101 | +- [Connection Strings in ADO.NET] |
| 102 | +- [ADO.NET Overview] |
| 103 | +::: |
| 104 | + |
| 105 | + |
| 106 | +[Npgsql]: https://www.npgsql.org/ |
0 commit comments