Open MarkMpn opened 2 years ago
This would be amazing. I often refer to this as preventing restatements.
Here is a humble thought :
string connectionString = "Your connection string";
using (var ctx = new SqlCDSContext(connectionString))
{
var updateRequest = new UpdateRequest();
var columnNames = new List<string>();
var newValues = new List<object>();
var tableName = "";
var whereClause = "";
//parse the update command
var match = Regex.Match(updateCommand, @"UPDATE (\w+) SET (.*) WHERE (.*)");
if (match.Success)
{
tableName = match.Groups[1].Value;
whereClause = match.Groups[3].Value;
var setClause = match.Groups[2].Value;
//split the set clause by comma
var setClauseList = setClause.Split(',');
foreach (var item in setClauseList)
{
//split the item by equal sign
var columnValue = item.Split('=');
columnNames.Add(columnValue[0].Trim());
newValues.Add(columnValue[1].Trim());
}
}
int i = 0;
while (i < columnNames.Count)
{
var currentValues = connection.QueryFirstOrDefault<Account>(
"SELECT " + columnNames[i] + " FROM " + tableName + " WHERE " + whereClause);
if (currentValues.Name != newValues[i])
{
updateRequest.Add(columnNames[i], newValues[i]);
}
i++;
}
ctx.Update(tableName).Set(updateRequest).Where(whereClause).Execute();
}
Option to only set a field if the new value is different to the existing value, e.g.
As the name hasn't changed, the
UpdateRequest
for this record would only contain thetelephone1
field.