Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 12th, 2011, 02:14 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ Update Statement

Hi

Does anyone know of any good web-based resources where I can get information on how to construct LINQ Update statements. I've done LINQ Select statements before with Imar's help but now I'm keen to look into LINQ Update statements instead of relying on standard SQL statements.

Thanks

Phil
 
Old April 12th, 2011, 02:58 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You may want to look at the 101 LINQ samples: http://msdn.microsoft.com/en-us/vcsharp/aa336746

However, Updates are not really part of LINQ; they are part of technologies that use LINQ such as LINQ to SQL or Entity Framework. Often all you need to do is call SaveChanges or SubmitChanges on a context that contains a modified entity,

What exactly are you trying to update?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 12th, 2011, 03:10 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ to SQL

Hi Imar

Thanks for the reply. I want to update the status of a record in my SQL database which is by default set to '1' when the user interact which the system and triggers the update, I want to update the corresponding record and change its status to '3', etc.

The numerical status codes do correspond to associated status descriptions within a foreign key relationship between my tables.

I would prefer to use LINQ updates rather than standard SQL as it seems alot 'cleaner' to implement within my build if that make sense?

Thanks

Phil
 
Old April 12th, 2011, 03:42 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I guess it all depends on what you're doing. Here's a simple example using LINQ to SQL but the idea is the same for EF (not sure which one you're using):

Code:
 
using (PlanetWroxDataContext context = new PlanetWroxDataContext())
{
  Person myPerson  = context.People.Where(p => p.Id == 123).First();
  myPerson.Whatever = 3;  // Update the property
  context.SubmitChanges();
}
Does this help? If not, you need to be more concrete about what you're doing and how.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; April 17th, 2011 at 03:08 PM..
The Following User Says Thank You to Imar For This Useful Post:
Phil Critchley (April 12th, 2011)
 
Old April 12th, 2011, 03:57 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ to SQL

Hi Imar

Thanks for the posting, it definately helps and explains what I'm after doing.

Thanks for your help.

Phil
 
Old April 12th, 2011, 05:45 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ to SQL

Hi Imar

I'm using Entity Framework. I can't get 'SubmitChanges' to implement with the EF and the only method I've found which is remotely the same is 'SaveChanges'. I'm assuming that both methods provide the same functionality and will update my backend SQL database?

Code:
Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
            Dim existingStatus = (From publication In myLiteratureCatalogue.Publications Where publication.PublicationID = "" Select publication).First()

            ' Change the status of the publication.
            existingStatus.StatusID = (1)

            myLiteratureCatalogue.SaveChanges()

        End Using
 
Old April 13th, 2011, 03:00 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Phil,
Quote:
I'm assuming that both methods provide the same functionality and will update my backend SQL database?
Yes, but in different frameworks. SubmitChanges is part of LINQ to SQL, while SaveChanges is part of Entity Framework 4. Both are similar ORM frameworks, where EF is getting more attention that L2S lately. I didn't know which one you were using so I mentioned both in my first post and then used L2S as an example later on.

Just realize that this code performs 2 queries: one to get the object, and another to update it. If you already had a reference to the said publication, you could update that instance instead.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Phil Critchley (April 13th, 2011)
 
Old April 13th, 2011, 05:09 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ to Entities

Hi Imar

Thanks very much for all the information you have provided me with. Its definately very beneficial to a novice like me who is just starting out with building ASP projects and not familiar with LINQ just the more traditional SQL.

Hopefully speak to you again soon

Phil
 
Old April 13th, 2011, 11:46 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome...

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 16th, 2011, 05:48 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ Update

Hi Imar

Is it possible to update a SQL Server database field with a NULL value using LINQ to Entities?

I'm trying to update a UniqueIdentifier field by getting the update to empty out the GUID. Is this possible?

Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
update multiple columns in an update statement debbiecoates SQL Server 2000 1 August 17th, 2008 04:01 AM
UPDATE statement takwirira ASP.NET 2.0 Basics 7 March 7th, 2007 07:48 PM
insert and/or update statement carswelljr SQL Language 3 September 28th, 2006 10:03 AM
Update Statement some error thas123 ADO.NET 1 January 8th, 2006 10:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.