简体   繁体   English

可观察的F#SQL插入

[英]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 我有一个SQL Server CE数据库,正在尝试使用事件中的值进行更新,但是当我尝试从可观察到的事件中进行插入时,控制台会打印出

< null > <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. 返回<null>的代码,也要注意最后的printfn不会执行。

    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. 我已经尝试过Linq的SubmitChanges() ,它的作用相同。

Here's the type used for the LINQ SubmitChanges: 这是用于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; F#中的unit类型是单例类型; 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 . ()值在编译后的代码中用null表示,这就是为什么您看到代码返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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