Skip to content

Save

Saves changes made to the data table to the database.

1
myDataTable.Save():DataTable

Returns

The current DataTable instance.

Remarks

Until this method is called, all changes to the data table are stored in memory. Save inserts added rows, updates changed rows and deletes rows marked as deleted.

For batch inserts or updates, get the table once, change rows in memory, and call Save once after the loop. Do not call $Database.EnsureData in that loop: it saves on each call. A single Save call does not imply a single SQL statement; the provider controls batching.

Tables returned by $Database.ExecuteQuery cannot be saved.

Examples

Update rows and save

1
2
3
4
5
6
7
var orders = $Database.GetData('Sales', 'Orders', 'Status', 'Draft');

orders.Each(function () {
    this.Status = 'Cancelled';
});

orders.Save();

Upsert a collection with stable IDs and one save

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Directory.Contacts has Id, DisplayName and Xml columns,
// and every Contact node has a non-empty, stable Id.
var contacts = $Database.Get({
    TargetSchema : 'Directory',
    TargetTable : 'Contacts'
});
var contactsById = contacts.ToDictionary('Id');

$Xml.SelectAll('Contacts/Contact', function (contact) {
    var id = contact.Evaluate('Id');

    contactsById.Ensure(id, {
        Id : id,
        DisplayName : contact.Evaluate('DisplayName'),
        Xml : contact.OuterXml()
    });
});

contacts.Save();
// running it again updates the same rows; unrelated rows are not deleted

See Also