コード例 #1
0
ファイル: SqlCommandFactory.cs プロジェクト: gburghardt/Bloog
        internal SqlCommand CreateUpdateStatement(Dictionary <string, PropertyChange> changes, string tableName, string keyName, object keyValue)
        {
            if (changes.Count == 0)
            {
                throw new InvalidOperationException("No changes detected");
            }

            var command = new SqlCommand();
            var columns = new List <string>();

            foreach (var change in changes)
            {
                columns.Add($"[{change.Key}] = @{change.Key}");
                command.Parameters.AddWithValue(change.Key, change.Value.NewValue);
            }

            columns.Add("[UpdatedBy] = @UpdatedBy");
            command.Parameters.AddWithValue("UpdatedBy", auditor.UserId);
            columns.Add("[UpdatedOn] = @UpdatedOn");
            command.Parameters.AddWithValue("UpdatedOn", auditor.Now());
            command.Parameters.AddWithValue(keyName, keyValue);
            command.CommandText = string.Format(UpdateStatementTemplate, tableName, string.Join(", ", columns), keyName);

            return(command);
        }