Skip to content

Save

Saves changes made to the data table to the database. For bulk persistence, apply row changes in memory and call Save once after the loop.

1
myDataTable.Save():DataTable

Returns

The current DataTable instance.

Remarks

Until the DataTable.Save method is called, all changes to the data table are stored in memory. The Save method flushes all changes to the database. For batch insert/update, obtain the table once, apply row changes in memory, then call Save once after the loop. Do not call $Database.EnsureData in that loop: it saves each call immediately. A single Save invocation does not imply a single SQL statement; the provider controls persistence batching.

Scan all rows and update the name of the groups

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var table = $Database.Get({
    Parameters : {
        TargetSchema : 'HR',
        TargetTable : 'Groups'
    }
});

table.Each(function () {
    this.Name = 'MyAdmin';
});

table.Save();

Upsert a collection with stable source IDs and one Save call

Directory.Contact must define Id, DisplayName and Xml columns. Each source Contact must already have a non-empty stable Id.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
var table = $Database.Get({
    Parameters: { TargetSchema: 'Directory', TargetTable: 'Contact' }
});
var byId = table.ToDictionary('Id');

$Xml.SelectAll('Contacts/Contact', function(contact) {
    var id = contact.Evaluate('Id');
    byId.Ensure(id, {
        Id: id,
        DisplayName: contact.Evaluate('DisplayName'),
        Xml: contact.OuterXml()
    });
});

table.Save();
The same IDs update existing rows on a later run. This example does not delete unrelated records. Keep this in-memory approach bounded by the expected collection size; large migrations may need explicit batches.

See Also