我有一个 F# 程序,它创建一个 DataTable
,用一行填充它,然后使用批量插入 (SqlBulkCopy
) 将数据写入 SQL Server。
这是我的代码:
open System
open System.Data
open System.Data.SqlClient
let lcpSqlConnection = new SqlConnection("<my-connection-string>")
lcpSqlConnection.Open()
let bulkLoadEsgData (conn:SqlConnection) (esgTable: list<byte * byte * int * byte * byte * single>) =
use sbc = new SqlBulkCopy(conn, SqlBulkCopyOptions.TableLock, null, BatchSize=10000, BulkCopyTimeout=1200, DestinationTableName="dbo.myTable")
sbc.WriteToServer(
let dt = new DataTable()
["Measure", typeof<byte>
"Identifier", typeof<byte>
"Simulation", typeof<int>
"Time", typeof<byte>
"Duration", typeof<byte>
"Result", typeof<single>]
|> List.iter (dt.Columns.Add>>ignore)
for esgData in esgTable do
let esg_measure, identifier, simulation, time, duration, result = esgData
let dr = dt.NewRow()
dr.["Measure"] <- esg_measure
dr.["Identifier"] <- identifier
dr.["Simulation"] <- simulation
dr.["Time"] <- time
dr.["Duration"] <- duration
dr.["Result"] <- result
dt.Rows.Add(dr)
dt)
let myList: list<byte * byte * int * byte * byte * single> = [(byte)1,(byte)1,1, (byte)1,(byte)1,(single)0.111]
// Call method to bulk insert data row
bulkLoadEsgData lcpSqlConnection myList
lcpSqlConnection.Close()
我认为我需要在 bulkLoadEsgData
方法中包含一个 for 循环,以使代码高效运行。但我不知道该做什么/在哪里写
最佳答案
我设法解决了这个问题
sbc.WriteToServer(
let dt = new DataTable()
dt.Columns.Add("Measure", typeof<byte>) |> ignore
dt.Columns.Add("Identifier", typeof<byte>) |> ignore
dt.Columns.Add("Simulation", typeof<int>) |> ignore
dt.Columns.Add("Time", typeof<byte>) |> ignore
dt.Columns.Add("Duration", typeof<byte>) |> ignore
dt.Columns.Add("Result", typeof<single>) |> ignore
for i= 1 to 100 do
dt.Rows.Add(i, i, i, i, i, (float)i*0.11) |> ignore
dt)
关于sql-server - 使用 SqlBulkCopy 将 F# DataTable 转换为 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14056891/