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:
DataClassesDataContext dataContext = new DataClassesDataContext(); //Create my new Movie record Movie movie = new Movie(); movie.Name = "Tim's movie"; //Insert the movie into the data context dataContext.Movies.InsertOnSubmit(movie); //Submit the change to the database dataContext.SubmitChanges();
And to delete do something like this:
DataClassesDataContext dataContext = new DataClassesDataContext(); var movies = from m in dataContext.Movies where m.Name == "Tim's movie" select m; dataContext.Movies.DeleteAllOnSubmit(movies); dataContext.SubmitChanges();