IDataStoreBase.UpdateAsync(UpdateSqlStrategy updateSqlStrategy, bool resetFlag, CancellationToken cancellationToken) Method
.NET Standard 2.x
Asynchronously updates the database with the changes made in the DataStore.
Namespace: DWNet.Data
Assembly: DWNet.Data.dll
Syntax
public Task<int> UpdateAsync(UpdateSqlStrategy updateSqlStrategy, bool resetFlag, CancellationToken cancellationToken)
Parameters
updateSqlStrategy
SnapObjects.Data.UpdateSqlStrategy
A value of the UpdateSqlStrategy
enumerated datatype.
resetflag
System.Boolean
A Boolean
value specifying whether DataStore should automatically reset the update flags:
True
-- To reset the flags.
False
-- Not to reset the flags.
cancellationToken
CancellationToken
A cancellation token that can be used by other objects or threads to receive notice of cancellation.
Returns
Task<int>
Returns a task that represents the asynchronous operation.
Remarks
Test success/failure code
It is a good practice to test the success/failure code after calling the UpdateAsync
method. You can also verify the number of rows inserted, updated, and deleted by a DataStore update by examining the values of the arguments of the UpdateEnd
event.
Updating several tables in one DataStore
If you want to update several tables in one DataStore, you can use the Modify
to change the Update
property of columns in each table. To preserve the status flags of the rows and columns, set the resetflag
argument to false
. Because the updates all occur in the same DataStore, you cannot allow the flags to be cleared until all the tables have used them. When all the updates are successfully completed and committed, you can call ResetUpdate
to clear the changed flags in the DataStore.
Updating multiple DataStores
If you are updating multiple DataStores as part of one transaction, set the resetflag
argument to false
. This will prevent the DataStore from "forgetting" which rows to update in case one of the updates fails. You can roll back, try to correct the situation, and update again. Once all of the DataStores have been updated successfully, use the DataContext.Commit
method to finalize the transaction and use ResetUpdate
to reset the DataStore's status flags.
Events
UpdateAsync
can trigger UpdateStart
and UpdateEnd
events.
Examples
The following code example adds a data record to the end of the DataStore
and then updates the change to the database. It uses a CancellationTokenSource
type parameter to make this operation cancelable.
using System;
using System.Threading;
using System.Threading.Tasks;
using DWNet.Data;
using SnapObjects.Data;
namespace Appeon.ApiDoc.IDataStoreBaseExamples
{
public class UpdateAsyncExample
{
private readonly SchoolContext _context;
public UpdateAsyncExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public async Task<int> Example6(bool resetFlag, CancellationTokenSource cts)
{
// Instantiates a DataStore object with datawindow: d_department.
var datastore = new DataStore("d_department", _context);
Console.WriteLine("Retrieved Rowcount: {0}", datastore.Retrieve());
// Modifies the value for the budget column in the first row.
datastore.SetItem(0, "budget", 330000.00m);
// Adds a row to the end of DataStore.
int row = datastore.AddRow();
datastore.SetItem(row, "Departmentid", 10);
datastore.SetItem(row, "Name", "New Department");
datastore.SetItem(row, "Budget", 10000m);
datastore.SetItem(row, "Startdate", DateTime.Now);
datastore.SetItem(row, "Administrator", 2);
int AffectedRow = 0;
try
{
// Call asynchronous update to commit the changes to the database.
// If a task has been cancelled, the call to the
// DataStore.UpdateAsync method throws an OperationCanceledException.
AffectedRow = await datastore.UpdateAsync(UpdateSqlStrategy.Update, resetFlag, cts.Token);
Console.WriteLine("After Add, Update and Retrieve, Rowcount: {0}", AffectedRow);
}
catch (OperationCanceledException e)
{
// The operation was cancelled before completion.
Console.WriteLine(e.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
cts.Dispose();
}
return AffectedRow;
/*This code produces the following output:
A task was canceled.
*/
}
}
}
Example Refer To
Model Class: D_Department
DataWindow File: d_department
Applies to
.NET Standard
2.x