0

How can you write an update statement to update a table in a database with the field being different for each update? and using Linq to SQL. Having searched StackOverFlow and google I find everything related to dynamic select statements, which I am not needing.

some information to help:

I have a table named Persons with typical fields: FirstName, LastName, Phone, Address, City, State, etc.

I currently do separate functions to perform an update to FirstName, next one to LastName, etc. I would like to combine these into 1 function that can update the given field to the given value and I am using Linq to Sql.

Let me know if this is still clear as mud.

Mud clearing up per comments: to clarify I am using Linq to Sql.

Code information:

bool UpdatePersonFirstName(string FirstName, Int PersonId)
{
    bool _wasUpdated = false;

    if (PersonId == 0)
          throw new ArguementMissingException();

    using (CustomerModelDataContext proxy = new CustomerModelDataContext())
    {
       try
        {
            var _result = proxy.Persons.SingleOrDefault(_per => _per.PersonId = PersonId)
            _result.FirstName = FirstName;
             proxy.SubmitChanges();
             _isUpdated = true;
         }
         catch (exception ex)
         { 
            throw ex;
          }
     }
     return _isUpdated;
}

bool UpdatePersonAddress(string AddressInfo, Int PersonId)
{
    bool _wasUpdated = false;

    if (PersonId == 0)
          throw new ArguementMissingException();

    using (CustomerModelDataContext proxy = new CustomerModelDataContext())
    {
       try
        {
            var _result = proxy.Persons.SingleOrDefault(_per => _per.PersonId = PersonId)
            _result.Address = AddressInfo;
            proxy.SubmitChanges();

             _isUpdated = true;
         }
         catch (exception ex)
         { 
            throw ex;
          }
     }
     return _isUpdated;
}

repeating the above for each updatable field in the Persons table.

I would like to accomplish the following:

void UpdatePersonTable(string FieldName, string FieldValue, int PersonId)
{
  ...
  var _result = Proxy.Persons.SingleOrDefault(_per => _per.PersonId = PersonId);
  _result.[Field matching FieldName] = FieldValue;
  Proxy.SubmitChanges();
  ...    }

to recap, i would like 1 method that will do the update based on passing in the field name and value vice having to make multiple update statements.

EDIT: Code updated to provide more of the functions methods. Proxy is just a name for the Model Data Context, as mentioned previously this is using Linq to Sql connections.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • 1
    It will be clear us mud until you'll present your problem in a form of a concise code example :) – BartoszKP Dec 17 '13 at 22:34
  • Can you show where you declare Proxy? I'm using an ObjectSet in my examples I have at hand, so the syntax might differ. – Francis Ducharme Dec 18 '13 at 01:04
  • in your code you declare a `_wasUpdated` but never use it and set and return a `_isUpdated`. Also I don't understand why you are even returning a `bool` because there is no code path where it could return false. Lastly, [never do `throw ex;`](http://stackoverflow.com/questions/730250/is-there-a-difference-between-throw-and-throw-ex) only do `throw;` or `throw new MoreSpcificExecption("Some Details", ex);` – Scott Chamberlain Dec 18 '13 at 18:28
  • the code was just being used as a very generic example and not meant to be complete code. In my environment I return a bool to indicate if an update was done or not so the calling method will know what to do. Also I throw a specific exception and the catch will return a false. The code was just enough to try to convey what i am trying to do. – Consulting Mechanic Dec 18 '13 at 18:33
  • Ok, even if you left out logic where it could return `false` you still should never do `throw ex;` you can use `ex` in your catch block to do your logging but when you re-throw the exception you should be throwing it with `throw;` – Scott Chamberlain Dec 18 '13 at 18:37
  • thank you for the information, however this has nothing to do with the original question. The code was put there to help explain what i am trying to accomplish and not as a means to debate code design or exception handling. Do you have an answer for the question? – Consulting Mechanic Dec 18 '13 at 19:16

1 Answers1

1

If you want to have one function performing all kinds of updates you could create a method that accepts a lamba expression as parameter

public void Update(Func<T> predicate)
{
    //Use 'predicate' against your data source...
    //Can't provide more code since I don't know if you are using EF, SqlClient, etc.
}
Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81