DCSIMG
Introducing to DLINQ – How to write data manipulation in DLINQ (Update, insert and delete) - part 3 - Wortzel's blog

Wortzel's blog

.Net (2.0, 3.0, 3.5), C#, Asp.net, Com+, GIS(ESRI Software), Management, Analysis & Design, Life, Trips, And more...
Introducing to DLINQ – How to write data manipulation in DLINQ (Update, insert and delete) - part 3

DLINK gives maximum flexibility in manipulation data with objects. You must retrieve the object before you update or delete it, and you must initialize a new object before inserting it into the db.

Let's see some examples:

 

Insert a new employee:

MyDbDataContext db = new MyDbDataContext();

 

// Init a new employee object using Object Initializer

Employee emp = new Employee()

{ Id = 10, Name = "New Employee", DepartmentCode = 5 };

db.Employees.Add(emp);

db.SubmitChanges();

 

You can see that I must create a new object before I added it to the Employee collection. After I added it, I must call the SubmitChanges method in order to commit the operation.

 

Delete an employee:

MyDbDataContext db = new MyDbDataContext();

 

Employee emp = db.Employees.Single(x => x.Id == 10);

db.Employees.Remove(emp);

db.SubmitChanges();

In this case the code is very similar to the insert operation (except that I retrieve the employee before).

 

Update an existing employee:

MyDbDataContext db = new MyDbDataContext();

 

Employee emp = db.Employees.Single(x => x.Id == 10);

emp.DepartmentCode = 1;

db.SubmitChanges();

In this case I retrieved an existing employee and changed his department code. In this case we didn't need to get throw the employee collection, we could update his details directly.

Read more:

Introducing to DLINQ

·   Introducing to DLINQ - Entities declaration - part 1

·   Introducing to DLINQ – How to write the queries - part 2

·   Introducing to DLINQ – How to write data manipulation in DLINQ (Update, insert and delete) - part 3

Advanced topics in DLINQ:

·   DLINQ – Advanced topics – Transaction support – part 4

·   DLINQ – Advanced topics – How it works in ASP.NET application – part 5

·   DLINQ – Advanced topics – Debug mode – part 6

Published Friday, August 10, 2007 9:50 AM by Avi Wortzel

Comments

# re: Introducing to DLINQ – how to write data manipulation in DLINQ (Update, insert and delete) - part 3@ Sunday, August 12, 2007 12:39 PM

hi avi,

what happens behind the scenes when the db.SubmitChanges() executed ?

does it generates some sort of sql statement to the database ?

Shimon krohkmal

# re: Introducing to DLINQ – how to write data manipulation in DLINQ (Update, insert and delete) - part 3@ Sunday, August 12, 2007 8:36 PM

Hi Shimon,

Yes, it creates Sql statements and execute them.

I'll explain it in my next post.

Avi Wortzel