Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically chose a field in a table to update
    primarykey
    data
    text
    <p>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.</p> <p>some information to help:</p> <p>I have a table named Persons with typical fields: FirstName, LastName, Phone, Address, City, State, etc.</p> <p>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.</p> <p>Let me know if this is still clear as mud.</p> <p>Mud clearing up per comments: to clarify I am using Linq to Sql.</p> <p>Code information:</p> <pre><code>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 =&gt; _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 =&gt; _per.PersonId = PersonId) _result.Address = AddressInfo; proxy.SubmitChanges(); _isUpdated = true; } catch (exception ex) { throw ex; } } return _isUpdated; } </code></pre> <p>repeating the above for each updatable field in the Persons table.</p> <p>I would like to accomplish the following:</p> <pre><code>void UpdatePersonTable(string FieldName, string FieldValue, int PersonId) { ... var _result = Proxy.Persons.SingleOrDefault(_per =&gt; _per.PersonId = PersonId); _result.[Field matching FieldName] = FieldValue; Proxy.SubmitChanges(); ... } </code></pre> <p>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.</p> <p><strong>EDIT:</strong> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload