如何使用F#在SQL Server中插入数据?

时间:2018-08-24 05:36:55

标签: f#

我想制作一个有助于从F#执行SQL查询的模块。我无法使用System.Data.SqlClient并出现错误。

module SQL_Helper

open System.Data.SqlClient

type SqlHelper (connection) =
    let exec bind parametres query = 
        use conn = new SqlConnection (connection)
        conn.Open()
        use cmd = new SqlCommand (query, conn)
        parametres |> List.iteri (fun i p -> 
                        cmd.Parameters.AddWithValue(sprintf "@p%d"  i, box p) |> ignore)
        bind cmd

    member __.Execute = exec <| fun c -> c.ExecuteNonQuery() |> ignore
    member __.Scalar  = exec <| fun c -> c.ExecuteScalar()
    member __.Read f  = exec <| fun c -> [ let read = c.ExecuteReader()
                                           while read.Read() do 
                                               yield f read ]

0 个答案:

没有答案