Join Ours Forum

Asp.Net,C#,Ajax,Sql server,Silverlight,WCF,WPF,NHibernate,Javascript codes exambles articles,Programming exambles.



-->
  • .NET Matters: Asynchronous HttpWebRequests, Interface Implementation, and More
  • Editor’s Note: New Technologies and a New Magazine
  • .NET Interop: Getting Started With IronRuby And RSpec, Part 1
  • Smart Client: Building Distributed Apps with NHibernate and Rhino Service Bus
  • Editor’s Note: Best Practices
  • New Stuff: Resources for Your Developer Toolbox
  • Data Points: LINQ Projection Queries and Alternatives in WCF Services
  • Concurrent Affairs: Solving The Dining Philosophers Problem With Asynchronous Agents
  • DCOM Interop: Generate Custom Managed C++ Wrappers for Easier COM Interoperation Using DCOMSuds
  • Editor’s Note: I Am The Business
  • Top 10 Articles,


    Silverlight Datagrid Select Update Delete Insert Asp.Net C#

    Differences Similarities Benefits Between Typed Datasets and Untyped Datasets asp.net c#

    Linq to Sql Introduction Entities Ado.Net C# SqlClasses Attributes Linq Mapping

    Linq Programming/How Linq Works?/Linq Implementation In Asp.Net C# Ado.Net

    Performing Developing Using Investigating Asp.Net 2.0 Ajax Application Development Asp.Net C#

    Hosting/Install Wcf Services in a Windows Service Asp.Net C#

    Connecting Silverlight to Wcf Asp.Net C#

    Silverlight Data Grid Data Binding WCF Asp.Net C#

    Invoking/Accessing/Calling WCF Service Without Adding/Creating Proxy/Reference Asp.Net C#

    Performing Doing Creating Insert Update Delete sql data Using Linq Database Asp.Net C#

    Performing Doing Creating Insert Update Delete sql data Using Linq Database Asp.Net C#

    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();
        }
    }

    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]

    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

    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

    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'$'

    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.

    Learn Easily Using Video Tutorials


    How to choose the right Java IDE – explained Eclipse NetBeans BlueJ

    Developing/Creating/Performing/Configuring Java Applications Using Eclipse IDE

    Step By Step Guide for Download/Install Configure Eclipse IDE for Java

    Editing data with the GridView control Asp.Net C#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Best way to prepare asp.net Interview - Success Stories

    Download Important Questions and PPT's:

    Sql Server Important Questions Online free download

    Dotnet Important Questions Online free download

    Exploring Linq to Sql Process Flow

    Learn how to perform silverlight programming

    Learn OOPs concepts in better and well manner

    Learn Ajax in better and well manner

    Leave a comment

    2 Comments.

    1. 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

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *


    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    Trackbacks and Pingbacks:

    Social Buttons by Linksku