Solved

LinqToSQL Update

Posted on 2013-12-06
5
186 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TFS 2015 Access denied. 1 34
Telerik RadEditor Control Save 8 39
Can Selenium do Load Testing? 2 59
How does GeoJson relates to Leaflet and Mapbox? 4 48
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 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