Snippets

Cameron Presley Type Provider - Providing a dynamic connection string during runtime

You are viewing an old version of this snippet. View the current version.
Revised by Cameron Presley 59126fd
// Need to have FSharp.Data.SqlClient package installed

open FSharp.Data

type Game = {id:int; name:string; numPlayers:int; playTime:int; publisher:string}

[<Literal>]
let connectionString = @"Server=.\SQLExpress;Database=testDb;Integrated Security=True"

type GetAllGames = SqlCommandProvider<"Select * from Games", connectionString>

type InsertGame = SqlCommandProvider<"Insert into Games (Id, Name, NumPlayers, PlayTime, Publisher) Values(@Id, @Name, @NumPlayers, @PlayTime, @Publisher)", connectionString>

let gameOne = {id=2; name="Coup"; numPlayers=5; playTime=15; publisher=""}

do
    use insertCmd = new InsertGame() // or new InsertGame('NewConnectionString')
    insertCmd.Execute(gameOne.id, gameOne.name, gameOne.numPlayers, gameOne.playTime, gameOne.publisher) |> ignore
    
    use cmd = new GetAllGames ()
    cmd.Execute () |> printfn "%A"
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.