Skip to content

Instantly share code, notes, and snippets.

@mikeball
Created February 13, 2017 09:00
Show Gist options
  • Save mikeball/015faea2d49c5a755e60157ea1e59098 to your computer and use it in GitHub Desktop.
Save mikeball/015faea2d49c5a755e60157ea1e59098 to your computer and use it in GitHub Desktop.
F#, dotnet core and postgreql query
open System
open Npgsql
type Category = {
id: int;
name: string
}
let categoriesSQL = "
select id, name from categories;
"
let getCategories =
use db = new NpgsqlConnection("Host=localhost;Username=dev;Password=dev;Database=myapp")
db.Open()
use cmd = new NpgsqlCommand(categoriesSQL, db)
use reader = cmd.ExecuteReader()
[while reader.Read() do
yield { id = reader.GetInt32(reader.GetOrdinal("id"))
name = reader.GetString(reader.GetOrdinal("name")) }]
[<EntryPoint>]
let main argv =
let cats = getCategories
for c in cats do
printf "%i :: %s\n" c.id c.name
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment