https://github.com/pimbrouwers/fssqlclient
Idiomatic F# wrappers for ADO.NET & SQL Server.
https://github.com/pimbrouwers/fssqlclient
Last synced: 25 days ago
JSON representation
Idiomatic F# wrappers for ADO.NET & SQL Server.
- Host: GitHub
- URL: https://github.com/pimbrouwers/fssqlclient
- Owner: pimbrouwers
- License: gpl-3.0
- Created: 2019-01-31T18:49:31.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-02-01T14:22:09.000Z (about 6 years ago)
- Last Synced: 2025-02-10T03:29:31.137Z (3 months ago)
- Language: F#
- Homepage:
- Size: 69.3 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FsSequel
Idiomatic F# wrappers for ADO.NET & SQL Server.
> `async` methods have not yet been added.
## Getting Started
The simplest way to get started quickly is to use the one of the three helper methods:
```f#
open FsSequel.Sqluse connection = createOpenConnection "your_connection_string"
use transaction = connection |> beginTransaction/// ExecuteNonQuery
execute (newCmd "update dbo.Table set Col1 = @col1 where Col1 = 2") [("col1", 3)] transaction
execute (newSproc "dbo.MySproc") [("param1", 3)] transaction/// ExecuteScalar
scalar (newCmd "select 1 as N") [] Convert.ToInt32 transaction/// Execute Reader
query (newCmd "select 1 as N union all select 2") [] (fun rd -> rd.GetValue(0) |> Convert.ToInt32) transaction/// AND Commit work
transaction |> commit/// OR Use built-in error handling
let sproc = execute (newSproc "dbo.MySproc") [("param1", 3)]
trycatch (transaction |> sproc)
|> commitOrRollback transaction
```## Beyond the basics
### Connections
```f#
open FsSequel.Sql/// The hard way
use connection =
"your_connection_string"
|> createConnection
|> passThrough openConnection/// Arbitrarily create a connection factory (hooray for >>)
let connectionFactory = createOpenConnection/// Simple connection string
use connection = connectionFactory "your_connection_string"/// OR using SqlConnectionStringBuilder
/// I find this method especially value for parsing command-line args as my connection string.
/// This defers its requirement to runtime and prevents it from needing to be stored anywhere.
let connectionString = createConnectionString "DataSource" "InitialCatalog" ["UserID"] ["Password"]
use connection = connectionFactory connectionString```
### Transaction
```f#
open FsSequel.Sqluse connection = createOpenConnection "your_connection_string"
/// Begin
use transaction = connection |> beginTransaction/// Save
transaction |> save "name"/// Undo
transaction |> undo "name"/// Commit
transaction |> commit/// Rollback
transaction |> rollback/// Commit or rollback based on Result<'a, 'b>
transaction |> commitOrRollback```
### Commands
```f#
open FsSequel.Sqllet connectionString = createConnectionString "DataSource" "InitialCatalog" ["UserID"] ["Password"]
let connectionFactory = createOpenConnection
use connection = connectionFactory connectrionString
use transaction = connection |> beginTransaction/// The direct way
use command =
transaction
|> createCommand "update dbo.Table set Col1 = @col1 where Col1 = 2"
|> passThrough (setCommandType System.Data.CommandType.Text)
|> passThrough (withParameters [("Col1", 1)]) // `passThrough` simply pipes turns 'a -> unit into 'a -> 'a/// The "typed" way
let sqlText = newCmd "update dbo.Table set Col1 = @col1 where Col1 = 2"
// OR let sprocName = newSproc "dbo.MySproc"use command =
transaction
|> createTypedCommand sqlText
|> passThrough (withParameters [("Col1", 1)]) // `passThrough` simply pipes turns 'a -> unit into 'a -> 'a/// The "typed" helper
let sqlText = newCmd "update dbo.Table set Col1 = @col1 where Col1 = 2"
use command = transaction |> createTypedCommandWithParameters sqlText [("Col1", 1)])/// And finally, do the work
command |> executeNonQuery
transaction |> commit/// Or if you want to handle exceptions
trycatch (command |> executeNonQuery)
|> commitOrRollback transaction
```#### Simple `Result<'a,'b>`-based Error Handling
```f#
use connection = createOpenConnection "your_connection_string"
use transaction = connection |> beginTransaction// Manual
let result = tryCatch(scalar "select 1 as N" [] Convert.ToInt32 transaction)match result with
| Ok res -> printfn "%A" res
| Error msg -> printfn "%s" msg// Pre-handled
tryCatch(scalar "select 1 as N" [] Convert.ToInt32 transaction)
|> commitOrRollback transaction
```### Bulk Copy
```f#
open FsSequel.Sqllet connectionString = createConnectionString "DataSource" "InitialCatalog" ["UserID"] ["Password"]
let connectionFactory = createOpenConnection
use connection = connectionFactory connectrionString
use transaction = connection |> beginTransactionuse reader = SOMETHING_IMPLEMENTING_IDataReader
let columnMappings = [("Col1", "Col1WithAnotherName")] // (Source Column * Destination Column)bulkInsert "dbo.Table" columnMappings reader connection
```