IDataStoreBase.Update(bool resetflag = true) Method
.NET Standard 2.x
Updates the database with the changes made in the DataStore.
Namespace: DWNet.Data
Assembly: DWNet.Data.dll
Syntax
public int Update(bool resetflag = true);
Parameters
resetflag
System.Boolean
A Boolean
value specifying whether DataStore should automatically reset the update flags:
True
(default) -- To reset the flags.
False
-- Not to reset the flags.
Returns
System.Int32
Returns the number of the rows affected if it succeeds, and -1
if canceled.
Remarks
You must use the constructor of DataStore or SetDataContext
method to specify the database connection before the Update
method is executed.
By default, Update
resets the update flags after successfully completing the update. However, you can prevent the flags from being reset until after you perform other validations and commit the changes. When you are satisfied with the update, call ResetUpdate
to clear the flags so that items are no longer marked as modified.
If you call the Update
method with the resetflag
argument set to false and do not call ResetUpdate
, the DataStore will attempt to issue the same SQL statements again the next time you call Update
.
Test success/failure code
It is good practice to test the success/failure code after calling the Update
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
Update
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.
using DWNet.Data;
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.IDataStoreBaseExamples
{
public class UpdateExample
{
private readonly SchoolContext _context;
public UpdateExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example1()
{
// 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", 220000.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);
// Calls Update to commit changes to database.
datastore.Update();
Console.WriteLine("After Add, Update and Retrieve, Rowcount: {0}",
datastore.Retrieve());
/*This code produces the following output:
Retrieved Rowcount: 4
After Add, Update and Retrieve, Rowcount: 5
*/
}
}
}
Example Refer To
Model Class: D_Department
DataWindow File: d_department
Applies to
.NET Standard
2.x