Click here to Skip to main content
15,608,989 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to update one column in LINQ to SQL. My Query is like :-
private void UpdateCourse()
      OperationDataContext OdContext = new OperationDataContext();
      //Get Single course which need to update
      COURSE objCourse = OdContext.COURSEs.Single(course => course.course_name == "B.Tech");
      //Field which will be update
      objCourse.course_desc = "Bachelor of Technology";
      // executes the appropriate commands to implement the changes to the database

My question is first it take record from COURSEs which match the criteria Than it update it. It this right approach? or SP is right solution here.

Updated 21-Jul-15 2:04am
Herman<T>.Instance 21-Jul-15 9:44am    
What is right approach? Both ways work. Which one suits you better?
_Dhull 22-Jul-15 0:19am    
Both are good and working but which one is efficient? Linq is hitting DB twice, once fro fetch record than update it. Which one is fast and efficient?
Herman<T>.Instance 22-Jul-15 4:33am    
your Sp has a query plan in the Db so that should performance better.

1. If you need to update a single value. Go for LINQ and update using Entity Framework.

int num = OdContext.SaveChanges();

It saves this modification to the database when we do OdContext.SaveChanges(). This method also returns the number of rows updated in the database.

2. For saving or updation of number of columns. Better to go for Store procedures if performance is a major concern.
Share this answer
It's Right :-) taht way you only update the course_desc field... What's the matter?
Share this answer
Yes both will work fine. But which one is fast and efficient? Suppose i have 15 column in table all are varchar(max) than LINQ TO SQL first fetch all column and than update means 2 hit to DB. Which one(above mention way OR Stored procedure) is fast and preferred?
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900