gpt4 book ai didi

linq - 如何将U放入F#类型提供者CRUD中?

转载 作者:行者123 更新时间:2023-12-04 03:45:07 25 4
gpt4 key购买 nike

CSDN(创建,读取,删除)的易于理解的示例出现在MSDN here

有一个不错的link on the page to a script可以建立一个测试数据库,我这样做了,并且很容易地获得了所有适用于CRD的示例。

页面上甚至还有一些方便的CRD子标题:

(创建行)http://msdn.microsoft.com/en-us/library/hh361033.aspx#BKMK_UpdateDB

(读取行)http://msdn.microsoft.com/en-us/library/hh361033.aspx#BKMK_QueryData

(删除行)http://msdn.microsoft.com/en-us/library/hh361033.aspx#BKMK_DeleteRows

名为BKMK_UpdateDB的那个不会在CRUD中执行U。它的名字叫Update,但是确实在CRUD中使用C语言。

如果我错过了where on this page,则会显示CRUD中的U,现在就射击我,然后退出阅读...

能请一位这里的上师提供一些帮助吗?

减轻上师的垃圾工作量:以下是与the MSDN web page上显示的代码差不多的代码。

只需在网页上运行test-database-create .sql script referred to,然后在下面的代码中为您的服务器和数据库名称编辑SqlDataConnection字符串,它就可以正常运行。

请注意,我对查询所做的唯一更改是仅更新了一行。现在,仅返回一行。看起来更简单的一行更改情况似乎更为重要。至少一次显示多次更改之前。

上师可以将最后4行更改为建议的F#-Type-Provider方法,以更改查询返回的数据,然后将更改后的行写到数据库中吗?

例如,将row.TestData1从10更改为11并将其写入db。

总结:MSDN page使我们F#-Type-Provider新手可以轻松进行CRUD中的CRD。

上师可以用正确/简便的F#-Type-Provider方法为我们的新手添加CRUD中的U吗?

非常感谢!

#r "System.Data.dll"
#r "FSharp.Data.TypeProviders.dll"
#r "System.Data.Linq.dll"
open System
open System.Data
open System.Data.Linq
open Microsoft.FSharp.Data.TypeProviders
open Microsoft.FSharp.Linq

type dbSchema = SqlDataConnection<"Data Source= --yourServer\yourInstance--;Initial Catalog= --YourTestDatabaseFromTheScript--;Integrated Security=SSPI;">
let db = dbSchema.GetDataContext()
let table1 = db.Table1

query { for row in db.Table1 do
where (row.TestData1 <= 10)
select row }
|> Seq.iter (fun row -> printfn "%d %s" row.TestData1 row.Name)

最佳答案

我还没有机会尝试新的query表达式-所以这只是一个猜测:

query { for row in db.Table1 do
where (row.TestData1 <= 10)
select row }
|> Seq.iter (fun row ->
// Update the row with some new value.
row.TestData1 <- row.TestData1 + 1)

// Now, call .SubmitChanges() to execute the SQL and update the database
try
db.DataContext.SubmitChanges()
printfn "Successfully updated the rows."
with
| exn -> printfn "Exception:\n%s" exn.Message

尽管在C#中, this page上的代码提供了另一个示例,说明了它如何工作。基本上,在这种情况下,F# query表达式只是包装了Linq-to-SQL。因此,如果我发布的代码不起作用,您应该看一下使用C#编写的Linq-to-SQL的新.NET 4.5示例。

关于linq - 如何将U放入F#类型提供者CRUD中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12906550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com