Solved

LinqToSQL Update

Posted on 2013-12-06
5
175 Views
Last Modified: 2013-12-27
I'm using the code below to update a record in a SQLCE database.  It seems strange that I have to fetch the record before I update it ?

     Using db = New sContext(ConnectionString)
                'find record
                Dim q = From s In db.Spots _
                Where s.sId = iId _
                Select s

                If q.Count() = 1 Then
                    Dim sp As SpotClass = (From s In db.Spots _
                        Where s.sId = iId _
                        Select s).First()
                    sp.sActive = "False"
                    sp.sEndDate = DateTime.Now.ToUniversalTime
                    db.SubmitChanges()
                End If
 

Open in new window


How would I update multiple records like in SQL.

UPDATE TBLTASK SET COMPLETEDATE = NOW WHERE TASKTYPE = 'AUTO'

Open in new window


(I hope that the answer is not what I'm suspecting!)
0
Comment
Question by:Dodsworth
  • 3
  • 2
5 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39702301
sContext.ExecuteCommand("UPDATE TBLTASK...")
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39702666
Really ?

Why would anyone use the chunk of code that I posted then ?

Can I use this with DDL ?
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39704247
LINQ-to-SQL (and Entity Framework) are designed to work on one entity at a time. If you've got a form where you are presenting a single instance of an object, letting the user change the values, and then pushing those changes to the database it works very well. It even works well if you've got a list of objects because the DataContext (or ObjectContext in EF) will track which objects and which columns have changed an only issue the necessary SQL.

However, if you want to do a bulk operation (e.g. update all rows that meet certain criteria, delete all rows that meet certain criteria) then LINQ-to-SQL and EF are the wrong tools. You could use them by retrieving a set containing all the objects you want to update, looping through the set and changing each one, then issuing SubmitChanges. The problems with that are that a SQL UPDATE will be issued for each changed object, so if you've got hundreds (or thousands) of objects that meet the criteria it will be incredibly inefficient vs. a single UPDATE statement and each UPDATE is performed in its own transaction instead of a single larger transaction.
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39707987
Can I use it to send DDL commands ?
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39710230
I'm not sure. In theory it should work if you sent a DDL statement through ExecuteCommand but it would probably be quicker just to throw a test together and try it. You've already got the DataContext and everything set up, it should only take a minute to try something simple. It's also getting beyond the scope of the original question.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now