Updating a Single Column in Linq to SQL using ExecuteCommand

Another way to update specific columns of a database record in Linq to SQL is use the ExecuteCommand method to execute any SQL statement in the database, bypassing Linq altogether:

using (var database = new DataContext())
    "UPDATE FooMax SET ID = {1} WHERE OID = {0}", OID, newID);

The advantage of this solution is that ExecuteCommand allows you to

  • dynamically create SQL statements
  • send the most complex statements to the database which might not easily be accomplished using Linq

The major drawback of the solution is that it shoots a big hole in your OR mapper and layered architecture:

  • Your C# code contains pieces of SQL statements as unverifiable strings
  • Changes in the database model may break existing C# code, noticed only during execution
  • Database abstraction is lost

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.