简体   繁体   中英

F# SQL Insert in Observable

I have a SQL Server CE database that I'm trying to update with values from an event, however when I try to do insert from the event observable, the console prints out

< null >

Does anyone know why this is?

Code that works outside observable:

    let test1 =
       use con  = new SqlCeConnection (@"Data Source=C:\Users\Database1.sdf")
       con.Open()
       let AA = "Enter"
       let BB = "Enter"
       use cmd = new SqlCeCommand("insert into Table1 (A,B) values (@A, @B)", con) 
       let A1 = new SqlCeParameter("A", "1")
       let B1 = new SqlCeParameter("B", "2")
       do cmd.Parameters.Add A1 |>ignore
       do cmd.Parameters.Add B1 |>ignore
       do cmd.ExecuteNonQuery() |>ignore
       printfn "%s" "complete1"
       ()

Code that returns < null > , also to note the printfn at the end does not execute.

    let sqlsubmit = stream|> Observable.map (fun x -> 
            use con  = new SqlCeConnection (@"Data Source=C:\Users\Database1.sdf")
            con.Open()
            let AA = "Enter"
            let BB = "Enter"
            use cmd = new SqlCeCommand("insert into Table1 (A,B) values (@A, @B)", con) 
            let A1 = new SqlCeParameter("A", "1")
            let B1 = new SqlCeParameter("B", "2")
            do cmd.Parameters.Add A1 |>ignore
            do cmd.Parameters.Add B1 |>ignore
            do cmd.ExecuteNonQuery |>ignore
            printfn "%s" "complete1"
            )

I've tried SubmitChanges() from Linq, and it does the same.

Here's the type used for the LINQ SubmitChanges:

    [<Table(Name = "Table1")>]
    type Macro (A:string, B:string)=    

    [<Column(IsPrimaryKey=true)>]
    member this.A = A
    [<Column>]
    member this.B = B

The unit type in F# is a singleton type; it's value is specified by () .

The () value is represented by a null within the compiled code, which is why you see your code return null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM