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.