tags:

views:

106

answers:

2

I have seen plenty of examples on how to query the database but nothing on how to update records. Below is the simple code that I wrote to retrieve a table, but can someone explain me how can I modify a field, say lastActiveDate, and update the table on the database

Thank you, suday

open System
open Microsoft.FSharp.Linq

let connString = "Server=localhost;Database=myDb;Trusted_Connection=True;"
let db = new MyDb(connString)
db.Log <- System.Console.Out

let res =
    Query.query <@ seq {
        for users in db.userAccounts do
        yield users
     } @>
     |> List.ofSeq

printfn "Totla users: %d" res.Length
A: 

LInQ (in any .NET language) is not used for doing insert/update/delete operations. After all, it Language Integrated Query. That having been said, in your example the db value should have methods for passing modified objects back to the database for persistence.

pblasucci
Thank you for your comments, I found some methods in the form of updateXXX (XXX instance);. I am assuming this is what you are referring to.I am totally new to the world of .NET, so forgive me for asking dumb questions
sudaly
Yes! Those are exactly the methods I meant. I would have been more specific, but its been more than a year since I've done LInQ-to-SQL.
pblasucci
+3  A: 

Since MyDB is a System.Data.Linq.DataContext, it tracks each object that it loads. Simply acquire an instance from MyDB, set a property's value on that instance, and call MyDB.SubmitChanges

David B
Thanks for your response David. I was reading the article "Using LINQ to SQL" (http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx) and found the same solution as yours. I tried it out and it worked perfectly
sudaly
Curioulsy - what is the sql the objects submit back to the server?
akaphenom
It's typically an UPDATE statement using optimistic concurrency... UPDATE TableNameSET columnName = value, columnName = value (foreach changed value)WHERE columnName = originalValue and columnName = originalValue (foreach original value)
David B