Created
February 13, 2017 09:00
-
-
Save mikeball/015faea2d49c5a755e60157ea1e59098 to your computer and use it in GitHub Desktop.
F#, dotnet core and postgreql query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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