Created
December 10, 2013 10:59
-
-
Save anderssonfilip/7888925 to your computer and use it in GitHub Desktop.
Example of using AsyncSeq{...} to asynchronously load similar data from tables
This file contains hidden or 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 Devart.Data | |
open Devart.Data.Oracle // http://www.devart.com/dotconnect/oracle/download.html | |
open FSharp.Control // https://github.com/tpetricek/FSharp.AsyncExtensions | |
open System | |
open System.Data | |
let sql = "SELECT * FROM {}" | |
let tables = [|"t1";"t2";"t3";"t4";"t5";"t6"|] | |
let cs = "<insert connection string here!>" | |
let LoadSingleTableAsync (cmd:IDbCommand) table = asyncSeq { | |
cmd.CommandText <- sql.Replace("{}", table) | |
let reader = cmd.ExecuteReader() | |
while reader.Read() do | |
yield reader.GetString(0) | |
} | |
let LoadAllTablesAsync (db:IDbConnection) = | |
let cmd = db.CreateCommand() | |
let mutable result = AsyncSeq.empty<string> | |
for r:AsyncSeq<string> in Seq.map (fun t -> LoadSingleTableAsync cmd t) tables do | |
result <- AsyncSeq.append result r | |
result | |
[<EntryPoint>] | |
let Main args = | |
use db = new OracleConnection(cs) | |
db.Open() | |
let items = LoadAllTablesAsync db | |
async{ | |
for item in items do | |
Console.WriteLine(items.ToString()) | |
} |> Async.Start | |
Console.ReadLine() |> ignore | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment