Using SqlBulkCopy with F# to export matrix in SQL

前端 未结 2 1769
轻奢々
轻奢々 2021-01-07 05:34

I want to transfer a large amount of data from F# to an SQL table. Basically my F# code creates a matrix of three columns (UserID, ProductID and price) and N li

相关标签:
2条回答
  • 2021-01-07 06:27

    If you're okay with third party libraries, fastmember can be really useful here.

    module DB =
        open System.Data
        open System.Data.SqlClient
        open FastMember
    
        // val bulk : conn:SqlConnection -> table:string -> columns:seq<string*string> -> items:seq<a'>
        let bulk conn table columns items =        
            use bcp = new SqlBulkCopy(connection = conn)
            bcp.EnableStreaming <- true
            bcp.DestinationTableName <- table
            for n,v in columns do   
                bcp.ColumnMappings.Add(new SqlBulkCopyColumnMapping(sourceColumn = n, destinationColumn = v))               
                |> ignore
            bcp.WriteToServer(items |> ObjectReader.Create)
    

    This leaves you with a generic method that you can just funnel any seq<a'> into. Along with some config parameters, which you can of course configure to your needs.

    0 讨论(0)
  • 2021-01-07 06:31

    This should get you started (and reference the documentation for more information about SqlBulkCopy):

    //you must reference System.Data and System.Xml
    open System.Data
    open System.Data.SqlClient
    
    let bulkLoadUserPurchases (conn:SqlConnection) (userPurchases: list<int * int * float>) =
        use sbc = new SqlBulkCopy(conn, SqlBulkCopyOptions.TableLock, null, BatchSize=500, BulkCopyTimeout=1200, DestinationTableName="YOUR_TABLE_NAME_HERE")
        sbc.WriteToServer(
            let dt = new DataTable()
            ["UserID", typeof<int>
             "ProductID", typeof<int>
             "Price", typeof<float>]
            |> List.iter (dt.Columns.Add>>ignore)
    
            for userPurchase in userPurchases do
                let userId, productId, price = userPurchase
                let dr = dt.NewRow()
                dr.["UserID"] <- userId
                dr.["ProductID"] <- productId
                dr.["Price"] <- price
                dt.Rows.Add(dr)
    
            dt)
    
    0 讨论(0)
提交回复
热议问题