Show / Hide Table of Contents

    ISqlExecutor.SelectOneAsync<TModel>(string sqlText, object[] parameters, CancellationToken cancellationToken) Method

    .NET Standard 2.x

    Asynchronously executes the SQL SELECT statement and returns the first row in the result set. All other rows are ignored.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    public Task<TModel> SelectOneAsync<TModel>(string sqlText, 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

    sqlText System.String

    A raw SQL statement which contains the parameter placeholders.

    The proper syntax of a parameter placeholder is specific to the data source (e.g., @newFirstName for SQL Server). To make a parameterized SQL ( e.g., update Person set FirstName = @newFirstName where PersonId = 1), these placeholders are filled in with the actual parameter values when the SQL statement is executed.

    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 each parameter identifier name appears in sqlText for the first time, 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<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 execute the SQL statement and put the first row of the result set to a model object. It uses a CancellationTokenSource type parameter to make this operation cancelable.

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using SnapObjects.Data;
    
    namespace Appeon.ApiDoc.ISqlExecutorExamples
    {
        public class SelectOneAsyncExample
        {
            private readonly SchoolContext _context;
            
            public SelectOneAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public async Task<DynamicModel> Example2(CancellationTokenSource cts)
            {
                // Defines the SQL SELECT statement.
                var sql = @"select CourseID, Title from Course where Credits = @id";
                
                int id = 4;
                DynamicModel result = null;
                
                try
                {
                    // If a task has been cancelled, the call to the 
                    // SqlExecutor.SelectOneAsync method throws an OperationCanceledException.
                    result = await _context.SqlExecutor
                        .SelectOneAsync<DynamicModel>(sql, new object[]{ id }, cts.Token);
                        
                    string title = result.GetValue<string>("Title");
                    
                    Console.WriteLine("Title: {0}", title);
                }
                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.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon