如何将U放入F#类型提供者CRUD中? [英] How to put the U in F# Type Provider CRUD?

查看:74
本文介绍了如何将U放入F#类型提供者CRUD中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Easy-to-follow examples of CRD (Create, Read, Delete) appear in MSDN here

页面上有一个很好的链接到脚本创建一个测试数据库,我这样做了,并轻松地使所有示例都适用于CRD.

There is a nice link on the page to a script to make a test database, and I did so, and easily got all the examples working for CRD.

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

There are even handy sub-headers on the page for CRD:

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

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

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

一个名为BKMK_UpdateDB的程序在CRUD中不执行U.它的名字叫Update,但确实在CRUD中使用C.

The one called BKMK_UpdateDB doesn't do the U in CRUD. Its name says Update, but it really does the C in CRUD.

如果我错过了在此页上的位置,则CRUD中的U是所示,现在就开枪我,然后退出阅读...

If I missed where on this page the U in CRUD is shown, just shoot me now and quit reading...

这里的一位老师可以提供一些帮助吗?

Could one of the guru's here please provide a little help?

要减轻上师的垃圾工作量:下面是几乎在 MSDN网页.

To lighten the junk-work-load for the guru's: Below is the code pretty much as it appears on the MSDN web page.

只需运行test-database-create .sql 引用的脚本在网页上,在下面的代码中为您的服务器和数据库名称编辑SqlDataConnection字符串,它应该可以正常运行.

Just run the test-database-create .sql script referred to on the web page, edit the SqlDataConnection string in the code below for your server and database-name, it should run fine.

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

Note that the only change I made to the query is to get just one row to update. Now exactly one row is returned. Seems more important to see the simple case of one row changed. At least that before showing multiple-changes-at-once.

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

Can a guru please change the last 4 lines into the recommended F#-Type-Provider way to make a change to the data returned by the query, and write that changed row out to the database?

例如,将row.TestData1从10更改为11,然后将其写入数据库.

For example, change row.TestData1 from 10 to 11 and write it to the db.

总结: MSDN页面使我们很容易F# -Type-Provider新手在CRUD中执行CRD.

Summing up: The MSDN page makes it easy for us F#-Type-Provider newbies to do the CRD in CRUD.

上师可以用正确/简便的F#-Type-Provider方法在CRUD中做U的新手吗?

Can a guru please fill us newbies in on the right/easy F#-Type-Provider way to do the U in CRUD?

非常感谢!

#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表达式-所以这只是一个猜测:

I haven't had a chance to try the new query expressions yet -- so this is just a guess:

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#中.基本上,在这种情况下,F#query表达式只是包装了Linq-to-SQL;因此,如果我发布的代码不起作用,您应该看看使用C#的Linq-to-SQL的新.NET 4.5示例.

The code on this page gives another example of how this works, albeit in C#. Basically, the F# query expression is (in this case) simply wrapping Linq-to-SQL; so if the code I posted doesn't work, you should take a look at some of the new .NET 4.5 examples of Linq-to-SQL with C#.

这篇关于如何将U放入F#类型提供者CRUD中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆