Solved

LinqToSQL Update

Posted on 2013-12-06
5
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

705 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