|
| 1 | +:::{include} /_include/links.md |
| 2 | +::: |
| 3 | + |
| 4 | +(connect-fsharp)= |
| 5 | +# F# |
| 6 | + |
| 7 | +:::{div} sd-text-muted |
| 8 | +Connect to CrateDB from F# .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 | +[Npgsql.FSharp] is a thin F#-friendly layer around it. |
| 17 | + |
| 18 | +:::{rubric} Synopsis |
| 19 | +::: |
| 20 | + |
| 21 | +`example.fsproj` |
| 22 | +```xml |
| 23 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 24 | + |
| 25 | + <PropertyGroup> |
| 26 | + <OutputType>Exe</OutputType> |
| 27 | + <TargetFramework>net$(NETCoreAppMaximumVersion)</TargetFramework> |
| 28 | + <GenerateProgramFile>false</GenerateProgramFile> |
| 29 | + </PropertyGroup> |
| 30 | + |
| 31 | + <ItemGroup> |
| 32 | + <PackageReference Include="Npgsql" Version="10.0.0-rc.1" /> |
| 33 | + <PackageReference Include="Npgsql.FSharp" Version="8.0.0" /> |
| 34 | + </ItemGroup> |
| 35 | + |
| 36 | + <ItemGroup> |
| 37 | + <Compile Include="example.fs" /> |
| 38 | + </ItemGroup> |
| 39 | + |
| 40 | +</Project> |
| 41 | +``` |
| 42 | +`example.fs` |
| 43 | +```f# |
| 44 | +open Npgsql.FSharp |
| 45 | +open System |
| 46 | +
|
| 47 | +let connString = "postgres://crate:crate@localhost:5432/doc?sslmode=disable"; |
| 48 | +
|
| 49 | +type Record = { |
| 50 | + mountain: string |
| 51 | + height: int |
| 52 | +} |
| 53 | +
|
| 54 | +[<EntryPoint>] |
| 55 | +let main args = |
| 56 | + let results = ResizeArray() |
| 57 | +
|
| 58 | + // Connect to database. |
| 59 | + connString |
| 60 | + |> Sql.connect |
| 61 | +
|
| 62 | + // Invoke basic query. |
| 63 | + |> Sql.query "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3" |
| 64 | + |> Sql.execute (fun read -> |
| 65 | + { |
| 66 | + mountain = read.text "mountain" |
| 67 | + height = read.int "height" |
| 68 | + }) |
| 69 | + |> results.Add |
| 70 | +
|
| 71 | + // Display results. |
| 72 | + printfn "%A" results |
| 73 | +
|
| 74 | + // Exit program successfully. |
| 75 | + 0 |
| 76 | +``` |
| 77 | + |
| 78 | +Start CrateDB using Docker or Podman, then invoke the example program. |
| 79 | +```shell |
| 80 | +docker run --rm --publish=5432:5432 crate '-Cdiscovery.type=single-node' |
| 81 | +``` |
| 82 | +```shell |
| 83 | +export DOTNET_CLI_TELEMETRY_OPTOUT=true |
| 84 | +dotnet run |
| 85 | +``` |
| 86 | + |
| 87 | +:::{rubric} CrateDB Cloud |
| 88 | +::: |
| 89 | + |
| 90 | +For connecting to CrateDB Cloud, use `Sslmode=require`, and |
| 91 | +replace hostname, username, and password with values matching |
| 92 | +your environment. |
| 93 | +```c# |
| 94 | +let connString = "postgres://admin:password@testcluster.cratedb.net:5432/doc?sslmode=require"; |
| 95 | +``` |
| 96 | + |
| 97 | +## See also |
| 98 | + |
| 99 | +:::{div} |
| 100 | +- [Connection Strings in ADO.NET] |
| 101 | +- [ADO.NET Overview] |
| 102 | +::: |
| 103 | + |
| 104 | + |
| 105 | +[Npgsql]: https://www.npgsql.org/ |
| 106 | +[Npgsql.FSharp]: https://github.com/Zaid-Ajaj/Npgsql.FSharp |
0 commit comments