LINQ - how i can create simple insert update using linq in asp.net

Asked By shoaib mansoori on 28-May-11 08:18 AM
Hi all
how i can create simple insert update delete application using linq in asp.net
please help me step by step
thanks
regards
Peter Bromberg replied to shoaib mansoori on 28-May-11 08:23 AM
LINQ itself doesn't do database "insert update and delete". You can use LINQ to SQL or Entity Framework to model and connect to your database, and LINQ to shape the queries.  There are plenty of tutorials on both of these technologies which are built in to Visual Studioi.
Ravi S replied to shoaib mansoori on 28-May-11 01:22 PM
hi

You have to use Linq to sql

refer the example below

For example, below is the NorthwindDataContext class that is persisted based on the model we designed above:

LINQ to SQL Code Examples

Once we've modeled our database using the LINQ to SQL designer, we can then easily write code to work against it.  Below are a few code examples that show off common data tasks:

1) Query Products From the Database

The code below uses LINQ query syntax to retrieve an IEnumerable sequence of Product objects.  Note how the code is querying across the Product/Category relationship to only retrieve those products in the "Beverages" category:

C#:

VB:

2) Update a Product in the Database

The code below demonstrates how to retrieve a single product from the database, update its price, and then save the changes back to the database:

C#:

VB:

Note: VB in "Orcas" Beta1 doesn't support Lambdas yet.  It will, though, in Beta2 - at which point the above query can be rewritten to be more concise.

3) Insert a New Category and Two New Products into the Database

The code below demonstrates how to create a new category, and then create two new products and associate them with the category.  All three are then saved into the database.

Note below how I don't need to manually manage the primary key/foreign key relationships. Instead, just by adding the Product objects into the category's "Products" collection, and then by adding the Category object into the DataContext's "Categories" collection, LINQ to SQL will know to automatically persist the appropriate PK/FK relationships for me. 

C#

VB:

4) Delete Products from the Database

The code below demonstrates how to delete all Toy products from the database:

C#:

VB:

TSN ... replied to shoaib mansoori on 28-May-11 02:45 PM
hi..

here are some of the goodlinks...

http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
http://weblogs.asp.net/scottgu/archive/2007/05/29/linq-to-sql-part-2-defining-our-data-model-classes.aspx
http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx
http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx
http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx
http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx
http://weblogs.asp.net/scottgu/archive/2007/08/23/linq-to-sql-part-7-updating-our-database-using-stored-procedures.aspx
http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part-8-executing-custom-sql-expressions.aspx
http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx
Riley K replied to shoaib mansoori on 28-May-11 10:47 PM
In the Button _Click() event handling routine type NorthwindDataContext ctx = new NorthwindDataContext();

      var myEmployee = ctx.Employees.Single(myemp => myemp.EmployeeID == 4);

      myEmployee.FirstName = "Julia";

      try
      {
        ctx.SubmitChanges();
        Response.Write("Updated");
      }
      catch (Exception ex)
      {

       Response.Write(ex.Message);
      }
      Load_Data();

    }


INSERT

In the Button2_Click() event handling routine type    NorthwindDataContext ctx = new NorthwindDataContext();

      Employee myemp = new Employee();
      myemp.LastName = "Kantzelis";
      myemp.FirstName = "Nikos";
      myemp.BirthDate = DateTime.Now.AddDays(-10000);
      myemp.HireDate = DateTime.Now.AddDays(-2000);
      myemp.HomePhone = "55555555";
      myemp.City = "Athens";
      myemp.Country = "Greece";

      ctx.Employees.InsertOnSubmit(myemp);

      try
      {
        ctx.SubmitChanges();
        Response.Write("Inserted");
      }
      catch (Exception ex)
      {

        Response.Write(ex.Message);
      }


DELETE

NorthwindDataContext ctx = new NorthwindDataContext();

      var myEmployee = ctx.Employees.Single(myemp => myemp.EmployeeID == 10);

      ctx.Employees.DeleteOnSubmit(myEmployee);
     
      try
      {
        ctx.SubmitChanges();
        Response.Write("Deleted");
      }
      catch (Exception ex)
      {

        Response.Write(ex.Message);
      }
Jitendra Faye replied to shoaib mansoori on 30-May-11 07:34 AM
You perform Insert, Update, and Delete operations in LINQ to SQL by adding, changing, and removing objects in your object model. By default, LINQ to SQL translates your actions to SQL and submits the changes to the database.

Example-

Northwnd db = new Northwnd(@"c:\Northwnd.mdf");
// Query for a specific customer.
var cust =
    (from c in db.Customers
     where c.CustomerID == "ALFKI"
     select c).First();
// Change the name of the contact.
cust.ContactName = "New Contact";
// Create and add a new Order to the Orders collection.
Order ord = new Order { OrderDate = DateTime.Now };
cust.Orders.Add(ord);
// Delete an existing Order.
Order ord0 = cust.Orders[0];
// Removing it from the table also removes it from the Customer’s list.
db.Orders.DeleteOnSubmit(ord0);
// Ask the DataContext to save all the changes.
db.SubmitChanges();
hope this will help you.

for more example follow this-

http://msdn.microsoft.com/en-us/library/bb386931.aspx