Skip to content

Instantly share code, notes, and snippets.

@pstephens
Created August 17, 2014 15:25
Show Gist options
  • Save pstephens/3bcd5690186a8245204d to your computer and use it in GitHub Desktop.
Save pstephens/3bcd5690186a8245204d to your computer and use it in GitHub Desktop.
Fun with F# and bulk copy
// Fun with F# and bulk copy
open System.Configuration
open System.Data
open System.Data.SqlClient
let getOpenConnection () =
let cn = new SqlConnection(ConfigurationManager.ConnectionStrings.["TdsScratch"].ConnectionString)
cn.Open ()
cn
[<EntryPoint>]
let main argv =
let tbl = new DataTable()
let fk1 = tbl.Columns.Add("FK1", typedefof<int>)
let fk2 = tbl.Columns.Add("FK2", typedefof<int>)
let successful = tbl.Columns.Add("Successful", typedefof<bool>)
use cn = getOpenConnection ()
use bulkcopy = new SqlBulkCopy(cn, SqlBulkCopyOptions.TableLock, null)
bulkcopy.DestinationTableName <- "TheTable"
bulkcopy.ColumnMappings.Add("FK1", "FK1") |> ignore
bulkcopy.ColumnMappings.Add("FK2", "FK2") |> ignore
bulkcopy.ColumnMappings.Add("Successful", "Successful") |> ignore
tbl.Rows.Add(1, 2, true) |> ignore
bulkcopy.WriteToServer(tbl)
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment