Skip to content

SqlBulkTools.FSharp provides F# computation expressions for SQL Server bulk insert, update, delete and merge/upsert operations.

License

Notifications You must be signed in to change notification settings

JordanMarr/SqlBulkTools.FSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SqlBulkTools

  • SqlBulkTools.FSharp is a wrapper around SqlBulkTools which provides F# computation expressions:

    • bulkInsert, bulkInsertAsync, bulkInsertTask
    • bulkUpdate, bulkUpdateAsync, bulkUpdateTask
    • bulkUpsert, bulkUpsertAsync, bulkUpsertTask
    • bulkDelete, bulkDeleteAsync, bulkDeleteTask
  • SqlBulkTools.FSharp v1.0.0 currently relies on a fork of SqlBulkTools by fretje which migrated to Microsoft.Data.SqlClient.

    • (Previous versions used System.Data.SqlClient.)
    • If you need to use System.Data.SqlClient, there is also a fork by fretje called SqlBulkTools.SystemDataSqlClient

Installation

NuGet version (SqlBulkTools.FSharp)

dotnet add package SqlBulkTools.FSharp

F# Computation Expressions!

open SqlBulkTools.FSharp open Microsoft.Data.SqlClient

Bulk Insert

use conn = new new SqlConnection("conn str..")

let count =
    bulkInsert conn {
        for user in users do
        table "Users"
        column user.Id
        columnMap user.FirstName "FName"
        columnMap user.LastName "LName"
        column user.SSN
        identity user.Id
    } 

Bulk Update

use conn = new new SqlConnection("conn str..")
// Explicit transaction is optional
use tx = conn.BeginTransaction()

bulkUpdate conn {
    for row in rows do
    transaction tx
    table (nameof ctx.Dbo.Orders)
    column row.Id
    column row.OrderDate
    column row.CustomerEmail
    column row.CustomerAddress
    column row.CustomerCity
    column row.CustomerState
    column row.CustomerZip
    matchTargetOn row.Id
}

if count > 0
then tx.Commit()
else tx.Rollback()

Bulk Upsert

bulkUpsert conn {
    for row in rows do
    table (nameof ctx.Dbo.Orders)
    column row.Id
    column row.OrderDate
    column row.CustomerEmail
    column row.CustomerAddress
    column row.CustomerCity
    column row.CustomerState
    column row.CustomerZip
    
    // to match against multiple columns:
    matchTargetOn row.OrderDate 
    matchTargetOn row.Email
}

Bulk Delete

bulkDelete conn {
    for sheet in deletedSheets do
    table "Sheets"
    column sheet.Id
    matchTargetOn sheet.Id
}

Original Project Readme

You can use the underlying C# fluent API if the F# computation expression builders don't meet your needs:

Please view the original C# README.md for full documentation:

About

SqlBulkTools.FSharp provides F# computation expressions for SQL Server bulk insert, update, delete and merge/upsert operations.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages