LINQ to SQL Inserts and Deletes
Inserting and Deleting records in a database using LINQ to SQL is just as easy as selecting information. What's not so easy is actually finding out how to do it. There are lots of excellent blog posts around such as this one by Scott Guthrie http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx, however most of them we're all written for the Beta version of LINQ to SQL which let you do a .Add() or .Remove() on your table, which was changed on the final release.
So to insert do something like this:
1DataClassesDataContext dataContext = new DataClassesDataContext(); 23//Create my new Movie record4Movie movie = new Movie();5movie.Name = "Tim's movie"; 67//Insert the movie into the data context8dataContext.Movies.InsertOnSubmit(movie); 910//Submit the change to the database11dataContext.SubmitChanges();
And to delete do something like this:
1DataClassesDataContext dataContext = new DataClassesDataContext();23var movies = from m in dataContext.Movies4 where m.Name == "Tim's movie"5 select m;67dataContext.Movies.DeleteAllOnSubmit(movies);89dataContext.SubmitChanges();