Show / Hide Table of Contents

    IDataStore<TModel>.RetrieveByKey(TModel model) Method

    .NET Standard 2.x

    Retrieves rows from the database. TModel's property with [Key] attributes are used to retrieve parameters in SQL SELECT statements in the DataStore.

    Namespace: DWNet.Data

    Assembly: DWNet.Data.dll

    Syntax

    int RetrieveByKey(TModel model);
    

    Parameters

    model TModel

    The TModel instance which will be set to the specified condition in the primary buffer of DataStore.

    Returns

    System.Int32

    Returns the number of rows retrieved (that is, rows in the primary buffer) if it succeeds, otherwise 0.

    This method always returns -1 if the data source is external. Use a method such as ImportJson to populate the DataStore.

    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 Retrieve 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, Retrieve 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 from the database for DataStore.

    using Appeon.ApiDoc.Models;
    using DWNet.Data;
    using System;
    
    namespace Appeon.ApiDoc.IDataStore_GenericExamples
    {
        public class RetrieveByKeyExample
        {
            private readonly SchoolContext _context;
    
            public RetrieveByKeyExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example()
            {
                // Instantiates a DataStore object with datawindow: d_department.
                var datastore = new DataStore<D_Department>(_context);
    
                var department = new D_Department { Departmentid = 1 };
    			
    			// Retrieves rows by key.
                datastore.RetrieveByKey(department);
    
                Console.WriteLine("Rowcount: {0}", datastore.RowCount);
                Console.WriteLine("DepartmentID: {0}; Name: {1}; Budget: {2}",
                      datastore[0].Departmentid, datastore[0].Name, datastore[0].Budget);
    
                /*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

    Back to top Generated by Appeon