Created
August 10, 2017 16:10
-
-
Save panesofglass/20108af09e332e1a6ec2af5f6c4b7aac to your computer and use it in GitHub Desktop.
TPL + single SqlConnection
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
#load "load-references-debug.fsx" | |
open System.Data.SqlClient | |
open FSharp.Data | |
[<Literal>] | |
let MasterCS = "Server=.;Database=master;Integrated Security=true;" | |
let createDB () = | |
use cmd = new SqlCommandProvider<"create database Test", MasterCS>(MasterCS) | |
cmd.Execute() |> ignore | |
let dropDB () = | |
use cmd = new SqlCommandProvider<"drop database Test", MasterCS>(MasterCS) | |
cmd.Execute() |> ignore | |
createDB() | |
[<Literal>] | |
let TestCS = "Server=.;Database=Test;Integrated Security=true;" | |
let createTable () = | |
use cmd = new SqlCommandProvider<"create table Test ([Id] INT PRIMARY KEY)", TestCS>(TestCS) | |
cmd.Execute() |> ignore | |
let dropTable () = | |
use cmd = new SqlCommandProvider<"drop table Test", TestCS>(TestCS) | |
cmd.Execute() |> ignore | |
// Tests | |
type TestDB = SqlProgrammabilityProvider<TestCS> | |
let showTestTable () = | |
use cmd = new SqlCommandProvider<"SELECT * FROM Test ORDER BY Id", TestCS>(TestCS) | |
for x in cmd.Execute() do printfn "%i" x | |
createTable() | |
let testConnInParallelLoop () = | |
let arr = [| 1..100 |] | |
let offsets = [| 0..1 |] | |
use conn = new SqlConnection(TestCS) | |
conn.Open() | |
let ids conn (offset:int) arr = | |
use table = new TestDB.dbo.Tables.Test() | |
arr | |
|> Array.Parallel.map ((+) offset) | |
|> Array.Parallel.iter (fun x -> | |
printfn "inserting %i" x | |
table.AddRow(Id = x)) | |
table.BulkCopy(conn) | |
offsets |> Array.Parallel.iter (fun offset -> ids conn offset arr) | |
conn.Close() | |
testConnInParallelLoop() | |
showTestTable() | |
dropTable() | |
dropDB() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment