Show / Hide Table of Contents

    ISqlExecutor.SelectAsync<TModel>(ISqlQueryBuilder queryBuilder, object[] parameters, CancellationToken cancellationToken) Method

    .NET Standard 2.x

    Asynchronously executes the SQL SELECT statement generated by an ISqlQueryBuilder object and returns a Task<IList<TModel>> object which represents the result set.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    public Task<IList<TModel>> SelectAsync<TModel>(ISqlQueryBuilder queryBuilder, object[] parameters, CancellationToken cancellationToken);
    

    Type Parameters

    TModel

    The datatype of the model object which represents the data row in the result set.

    You can also specify DynamicModel to TModel if you do not want to define a custom model class.

    Parameters

    queryBuilder SnapObjects.Data.ISqlQueryBuilder

    A SqlQueryBuilder object which can be used to generate a SQL SELECT statement.

    parameters System.Object[]

    One or more objects which contain the values corresponding to the SQL parameter identifier names.

    You can also pass the arguments directly in the order in which they appear in the SQL SELECT statement generated by queryBuilder without using the objects

    cancellationToken CancellationToken

    A cancellation token that can be used by other objects or threads to receive notice of cancellation..

    Returns

    Task<IList<TModel>>

    Returns a task that represents the asynchronous operation.

    Remarks

    You can use parameter placeholders in the raw SQL Statement. It is recommended to use the syntax for parameter placeholders that is specific to the data source. e.g., uses @parametername for SQL Server and uses :parametername for Oracle.

    Examples

    The following code example demonstrates how to use the SelectAsync<TModel> (ISqlQueryBuilder, params object[], CancellationTokenSource) method to build and execute the SQL statement using SqlQueryBuilder and return the result to a list of models.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    using SnapObjects.Data;
    
    namespace Appeon.ApiDoc.ISqlExecutorExamples
    {
        public class SelectAsyncExample
        {
            private readonly SchoolContext _context;
            
            public SelectAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public async Task<IList<DynamicModel>> Example4(CancellationTokenSource cts)
            {
                // Creates a SqlQueryBuilder.
                var sql = new SqlQueryBuilder();
                
                // Defines the select list.
                string[] columns = { "CourseID", "Title" };
                
                // Builds the SQL statement.
                sql.Select(columns)
                   .From("Course")
                   .Where("CourseID", SqlBuilder.Parameter<int>("id"));
                   
                int id = 1061;
                IList<DynamicModel> result = null;
                
                try
                {
                    // If a task has been cancelled, the call to the 
                    // SqlExecutor.SelectAsync method throws an OperationCanceledException.
                    result = await _context.SqlExecutor
                        .SelectAsync<DynamicModel>(sql, new object[] { id }, cts.Token);
                        
                    string title = result.FirstOrDefault().GetValue<string>("Title");
                    
                    Console.WriteLine("Title: {0}", title);
                }
                catch (OperationCanceledException e)
                {
                    // If it runs to this point, 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.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon