IDataStoreBase.RetrieveByKeyAsync(object[] parameters, CancellationToken cancellationToken) Method
.NET Standard 2.x
Asynchronously retrieves rows from the database. You can specify a value for a property with the [Key]
attribute to retrieve rows to the DataStore.
Namespace: DWNet.Data
Assembly: DWNet.Data.dll
Syntax
public Task<int> RetrieveByKeyAsync(object[] parameters, CancellationToken cancellationToken)
Pararmeters
parameters
System.Object[]
The value for the property with the [Key]
attribute.
NOTE: This parameter is required.
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
After rows are retrieved, the DataStore's filter is applied. Therefore, any retrieved rows that do not meet the filter criteria are immediately moved to the filter buffer and are not included in the return count.
Before you can retrieve rows for the DataStore, you must specify a DataContext
object with the constructor of DataStore or SetDataContext
method to establish a database connection.
Normally, when you call the RetrieveByKeyAsync
method, any rows that are already in the DataStore are discarded and replaced with the retrieved rows. You can set DwRetrieveEventArgs.IsResetBuffer
to false
in the RetrieveStart
event to prevent this. In this case, RetrieveByKeyAsync
adds any retrieved rows to the ones that already exist in the buffers.
Examples
The following code example demonstrates how to retrieve rows by key asynchronously from the database for DataStore
. It uses a CancellationTokenSource
type parameter to make this operation cancelable.
using System;
using DWNet.Data;
using System.Threading.Tasks;
using Appeon.ApiDoc.Models;
using System.Threading;
namespace Appeon.ApiDoc.IDataStoreBaseExamples
{
public class RetrieveByKeyAsyncExample
{
private readonly SchoolContext _context;
public RetrieveByKeyAsyncExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public async Task<int> Example2(CancellationTokenSource cts)
{
// Instantiates a DataStore object with datawindow: d_department.
var datastore = new DataStore("d_department", _context);
int result = 0;
try
{
// Retrieves rows by key (DepartmentId = 1).
// If a task has been cancelled, the call to the
// DataStore.RetrieveByKeyAsync method throws an OperationCanceledException.
result = await datastore.RetrieveByKeyAsync(1, cts.Token);
}
catch (OperationCanceledException e)
{
// The operation was cancelled before completion.
Console.WriteLine(e.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
cts.Dispose();
}
return result;
/*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