Description: Learn How To Insert, Update, Delete Using Linq To SQL. Best Dot Net Training
LINQ – Language Integrated Query is a way to write queries on various data sources especially the collection of objects, SQL database, XML document. The queries syntax is very similar to SQL queries and includes similar features like sorting, filtering, grouping, joining and calculating. LINQ now is an integral part of C# language features and internals of it are depended on Extension methods and delegates topics.
Insert Method for Data Linq to SQL:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public void InsertCustomer(Customer objCustomer) { \\this is your context object VisionEngineersDBEntities context = new VisionEngineersDBEntities(); objCustomer.DateCreated = DateTime.Now; objCustomer.FKCreatedByUserId = Helper.UserId;//Helper User Id context.Customers.Add(objCustomer); context.SaveChanges(); } |
Update Method for Data Linq to Sql:
1 2 3 4 5 6 7 8 9 10 11 12 |
public void UpdateCustomer(Customer objCustomer) { VisionEngineersDBEntities context = new VisionEngineersDBEntities(); objCustomer.DateModified = DateTime.Now; objCustomer.FKModifiedByUserId = Helper.UserId; context.Entry(objCustomer).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } |
Delete Method:
1 2 3 4 5 6 7 8 9 10 |
public void DeleteCustomer(int customerId) { VisionEngineersDBEntities context = new VisionEngineersDBEntities(); Customer objCustomer = context.Customers.Find(customerId); context.Customers.Remove(objCustomer); context.SaveChanges(); } |
Please follow and like us: