IDataStoreBase.RetrieveByKeyAsync(params object[] arguments) 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(params object[] parameters)
Pararmeters
parameters
System.Object[]
The value for the property with the [Key]
attribute.
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
.
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> Example1()
{
// 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).
result = await datastore.RetrieveByKeyAsync(1);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
// Gets the first row of data from the DataStore.
var department = datastore.GetModel<D_Department>(0);
Console.WriteLine("Rowcount: {0}", datastore.RowCount);
Console.WriteLine("DepartmentID: {0}; Name: {1}; Budget: {2}",
department.Departmentid, department.Name, department.Budget);
return result;
/*This code produces the following output:
Rowcount: 1
DepartmentID: 1; Name: Engineering; Budget: 350000.0000
*/
}
}
}
Example Refer To
Model Class: D_Department
DataWindow File: d_department
Applies to
.NET Standard
2.x