Introduction:
In this article,i am going to explain about how to perform sql data update delete using linq api.
Main:
The DataContext implements a service known as object tracking. Object tracking is the change tracking
system that LINQ to SQL provides to detect whether an object has been changed after it has been retrieved
from the database.
When working with object tracking, it is important to realize that DataContext needs to be aware of every
insert, update, and delete to generate the appropriate SQL statement.
public void UpdateProducts()
{
using ( var db = new DataContext() )
{
var query = from p in db.Products
select p;
foreach(var product in query)
{
product.ListPrice = product.ListPrice * 1.1;
if ( product.ListPrice > 400 )
{
db.Products.DeleteOnSubmit( product );
}
}
ProductType game = (from pt in db.ProductTypes
where pt.ProductTypeName == "Game"
select pt).Single();
Product newProduct = new Product()
{
ProductName = "Travian",
ListPrice = 20,
ProductType = game,
ProductDescription = "Online Game",
ProductTypeID = 1,
ListPriceCurrency = "$"
};
newProduct.ListPrice = 5;
db.Products.InsertOnSubmit( newProduct );
db.SubmitChanges();
}
}
public void UpdateProducts() { using ( var db = new DataContext() ) { var query = from p in db.Products select p; foreach(var product in query) { product.ListPrice = product.ListPrice * 1.1; if ( product.ListPrice > 400 ) { db.Products.DeleteOnSubmit( product ); } } ProductType game = (from pt in db.ProductTypes where pt.ProductTypeName == "Game" select pt).Single(); Product newProduct = new Product() { ProductName = "Travian", ListPrice = 20, ProductType = game, ProductDescription = "Online Game", ProductTypeID = 1, ListPriceCurrency = "$" }; newProduct.ListPrice = 5; db.Products.InsertOnSubmit( newProduct ); db.SubmitChanges(); } } |
This code shows how to use VideoGameStoreDataContext to query a list of products. Next, the price for each product is increased by 10 percent and any product that costs more than $400 is deleted. Finally, a new product is added. A number of dynamic SQL statements are generated, which are described in the next sections.
SELECT STATEMENT
The generated SELECT statement looks as follows:
SELECT [t0].[ProductID], [t0].[ProductName], [t0].[ProductDescription],
[t0].[ListPrice], [t0].[ProductTypeID], [t0].[SupplierID], [t0].[ReleaseDate],
[t0].[ProductImageUrl], [t0].[ListPriceCurrency], [t0].[ProductVersion]
FROM [dbo].[Product] AS [t0]
SELECT [t0].[ProductID], [t0].[ProductName], [t0].[ProductDescription], [t0].[ListPrice], [t0].[ProductTypeID], [t0].[SupplierID], [t0].[ReleaseDate], [t0].[ProductImageUrl], [t0].[ListPriceCurrency], [t0].[ProductVersion] FROM [dbo].[Product] AS [t0] |
UPDATE STATEMENT
For each product, a variation of the following UPDATE statement is executed when SubmitChanges is called. Notice that the query uses the timestamp (= row version) on the product object in the WHERE clause to implement optimistic concurrency. A SELECT statement is generated to update the timestamp property of the product after the product has been updated:
exec sp_executesql N'UPDATE [dbo].[Product]
SET [ListPrice] = @p2
WHERE ([ProductID] = @p0) AND ([ProductVersion] = @p1)
SELECT [t1].[ProductVersion]
FROM [dbo].[Product] AS [t1]
WHERE ((@@ROWCOUNT) > 0) AND ([t1].[ProductID] = @p3)',
N'@p0 int,@p1 timestamp,@p2 float,
@p3 int',@p0=4,@p1=0x0000000000001696,@p2=39.930000000000007,@p3=4
SELECT [t1].[ProductVersion]'
FROM [dbo].[Product] AS [t1]
WHERE ((@@ROWCOUNT) > 0) AND ([t1].[ProductID] = @p3)',
N'@p0 int,@p1 timestamp,@p2 float,
@p3 int',@p0=4,@p1=0x0000000000001696,@p2=39.930000000000007,@p3=4
exec sp_executesql N'UPDATE [dbo].[Product] SET [ListPrice] = @p2 WHERE ([ProductID] = @p0) AND ([ProductVersion] = @p1) SELECT [t1].[ProductVersion] FROM [dbo].[Product] AS [t1] WHERE ((@@ROWCOUNT) > 0) AND ([t1].[ProductID] = @p3)', N'@p0 int,@p1 timestamp,@p2 float, @p3 int',@p0=4,@p1=0x0000000000001696,@p2=39.930000000000007,@p3=4 SELECT [t1].[ProductVersion]' FROM [dbo].[Product] AS [t1] WHERE ((@@ROWCOUNT) > 0) AND ([t1].[ProductID] = @p3)', N'@p0 int,@p1 timestamp,@p2 float, @p3 int',@p0=4,@p1=0x0000000000001696,@p2=39.930000000000007,@p3=4 |
DELETE STATEMENT
The generated DELETE statement looks as follows:
exec sp_executesql N'DELETE FROM [dbo].[Product] WHERE ([ProductID] = @p0) AND
([ProductVersion] = @p1)',N'@p0 int,@p1 timestamp',@p0=2,@p1=0x00000000000019A1
exec sp_executesql N'DELETE FROM [dbo].[Product] WHERE ([ProductID] = @p0) AND ([ProductVersion] = @p1)',N'@p0 int,@p1 timestamp',@p0=2,@p1=0x00000000000019A1 |
INSERT STATEMENT
Again, notice that after the INSERT statement, the table is queried to return the timestamp of the product:
exec sp_executesql N'INSERT INTO [dbo].[Product]([ProductName], [ProductDescription],
[ListPrice], [ProductTypeID], [SupplierID], [ReleaseDate], [ProductImageUrl],
[ListPriceCurrency])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7)
SELECT [t0].[ProductID], [t0].[ProductVersion]
FROM [dbo].[Product] AS [t0]
WHERE [t0].[ProductID] = (SCOPE_IDENTITY())',N'@p0 nvarchar(7),@p1 nvarchar(11),
@p2 float,@p3 int,@p4 int,@p5 datetime,@p6 nvarchar(4000),
@p7 nvarchar(1)',@p0=N'Travian',
@p1=N'Online Game',@p2=5,@p3=1,@p4=NULL,@p5=NULL,@p6=NULL,@p7=N'$'
exec sp_executesql N'INSERT INTO [dbo].[Product]([ProductName], [ProductDescription], [ListPrice], [ProductTypeID], [SupplierID], [ReleaseDate], [ProductImageUrl], [ListPriceCurrency]) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7) SELECT [t0].[ProductID], [t0].[ProductVersion] FROM [dbo].[Product] AS [t0] WHERE [t0].[ProductID] = (SCOPE_IDENTITY())',N'@p0 nvarchar(7),@p1 nvarchar(11), @p2 float,@p3 int,@p4 int,@p5 datetime,@p6 nvarchar(4000), @p7 nvarchar(1)',@p0=N'Travian', @p1=N'Online Game',@p2=5,@p3=1,@p4=NULL,@p5=NULL,@p6=NULL,@p7=N'$' |
Note that SubmitChanges first executes all INSERT statements, then all UPDATE statements, and finally all DELETE statements. The updates are not performed as a single transaction. If transactional behavior is needed, then the SubmitChanges method needs to be placed within a TransactionScope.
Conclusion:
Hope this helps,
Happy Coding.
found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later