Wahid Masud
Wahid Masud

Reputation: 1093

F# Parameterized Insert Sql

Trying to understand how to parameterize query with SqlCommandProvider. So far I've got this

[<Literal>]
let connectionString = @"Data Source=.\SQL2019;Initial Catalog=MyDb;Trusted_Connection=True;"

type InsertCommand = 
    SqlCommandProvider<"INSERT INTO Category (Name, Value, Website) VALUES (@name, @value, @website)", connectionString, SingleRow = true>

let cmdInsert = new InsertCommand()

let insertCategories (data: seq<string * string>) =
    data
    |> Seq.iter
        (
            fun x ->
                cmdInsert.Execute(name = fst x, value = Int32.Parse(snd x), website = "www.example.com") |> ignore
        )

But on the line new InsertCommand() I'm getting this error

The member or object constructor 'SqlCommandProvider,CommandText="...",ConnectionStringOrName="...",SingleRow="True"'
 does not take 0 argument(s). An overload was found taking 2 arguments

Can someone please explain? If not, can someone please give an example on how to do parameterized insert query?

Upvotes: 1

Views: 90

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243061

The error message is telling you that the you are missing an argument for the constructor of the provided type, i.e. your InsertCommand type. I do not have a SQL database to try this, but the type provided by SqlCommandProvider should have an overload that takes a connection string and (optionally) a timeout.

The following should do the trick:

let cmdInsert = new InsertCommand(connectionString)

Upvotes: 1

Related Questions