Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put the U in F# Type Provider CRUD?

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

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.

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

(Create rows) http://msdn.microsoft.com/en-us/library/hh361033.aspx#BKMK_UpdateDB

(Read rows) http://msdn.microsoft.com/en-us/library/hh361033.aspx#BKMK_QueryData

(Delete rows) http://msdn.microsoft.com/en-us/library/hh361033.aspx#BKMK_DeleteRows

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

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?

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

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.

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?

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

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

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

Many thanks!

#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)
like image 425
brucer10 Avatar asked Oct 16 '12 02:10

brucer10


1 Answers

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

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#.

like image 115
Jack P. Avatar answered Nov 05 '22 23:11

Jack P.