|
| 1 | +(connect-dotnet)= |
| 2 | + |
| 3 | +# .NET |
| 4 | + |
| 5 | +:::{div} sd-text-muted |
| 6 | +Use Npgsql to connect to CrateDB from .NET applications. |
| 7 | +::: |
| 8 | + |
| 9 | +:::{rubric} About |
| 10 | +::: |
| 11 | + |
| 12 | +[Npgsql] is an open source ADO\.NET Data Provider for PostgreSQL, for programs |
| 13 | +written in C#, Visual Basic, and F#. |
| 14 | + |
| 15 | +:::{rubric} Synopsis |
| 16 | +::: |
| 17 | + |
| 18 | +```c# |
| 19 | +using Npgsql; |
| 20 | + |
| 21 | +var connString = "Host=localhost;Username=crate;Password=crate;Database=testdrive"; |
| 22 | + |
| 23 | +var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString); |
| 24 | + |
| 25 | +await using var dataSource = dataSourceBuilder.Build(); |
| 26 | +await using var conn = await dataSource.OpenConnectionAsync(); |
| 27 | + |
| 28 | +await using var cmd = new NpgsqlCommand("SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3", conn); |
| 29 | +await using var reader = await cmd.ExecuteReaderAsync(); |
| 30 | +while (await reader.ReadAsync()) |
| 31 | + Console.WriteLine(reader.GetString(0), reader.GetString(1)); |
| 32 | +``` |
| 33 | + |
| 34 | +## Examples |
| 35 | + |
| 36 | +:::{card} |
| 37 | +:link: https://github.com/crate/cratedb-examples/tree/main/by-language/csharp-npgsql |
| 38 | +:link-type: url |
| 39 | +{material-outlined}`play_arrow;2em` |
| 40 | +Connect to CrateDB and CrateDB Cloud using .NET (C#) |
| 41 | ++++ |
| 42 | +Demonstrates a basic example using Npgsql with CrateDB. |
| 43 | +::: |
| 44 | + |
| 45 | +:::{card} |
| 46 | +:link: https://github.com/crate/cratedb-examples/tree/main/by-language/csharp-efcore |
| 47 | +:link-type: url |
| 48 | +{material-outlined}`play_arrow;2em` |
| 49 | +Connect to CrateDB and CrateDB Cloud using the Npgsql Entity Framework |
| 50 | ++++ |
| 51 | +Demonstrates the Npgsql Entity Framework Core provider for PostgreSQL with CrateDB. |
| 52 | +::: |
| 53 | + |
| 54 | +[](https://github.com/crate/cratedb-examples/actions/workflows/lang-csharp-npgsql.yml) |
| 55 | +[](https://github.com/crate/cratedb-examples/actions/workflows/lang-csharp-efcore.yml) |
| 56 | + |
| 57 | + |
| 58 | +[Npgsql]: https://www.npgsql.org/ |
0 commit comments