Show / Hide Table of Contents

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

    .NET Standard 2.x

    Asynchronously executes a SQL stored procedure and returns a Task<IList<TModel>> object which represents the result set.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    public  Task<IList<TModel>> SelectProcedureAsync<TModel>(string procedureName, 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

    procedureName System.String

    The name of the SQL stored procedure.

    parameters System.Object[]

    The parameters for executing the SQL stored procedure.

    One or more objects, containing the parameter direction and values, which correspond to the SQL stored procedure's parameters.

    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.

    Examples

    The following code example demonstrates how to use the SelectProcedureAsync method to execute the specified procedure in the database and return the query result to a list of models. It uses a CancellationTokenSource type parameter to make this operation cancelable.

    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 SelectProcedureAsyncExample
        {
            private readonly SchoolContext _context;
            
            public SelectProcedureAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public async Task<IList<DynamicModel>> Example2(CancellationTokenSource cts)
            {
                // Specifies the procedure to be executed.
                // It returns the DepartmentName .
                string procedureName = "GetDepartmentName";
                
                /* StoredProcedure GetDepartmentName:
                
                CREATE PROCEDURE[dbo].[GetDepartmentName]
                @ID int,
                @Name nvarchar(50) OUTPUT
                AS
                SELECT @Name = Name FROM Department
                WHERE DepartmentID = @ID
                
                GO
                */
                
                // Sets the parameter values. The parameter direction defaults to 
                // ParameterDirection.Input, so you can omit it.
                ParamValue idParam = ParamValue.New<string>("id", "4");
                
                // The parameter 'Name' is Output type.
                ParamValue nameParam = ParamValue.Output<string>("name");
                
                IList<DynamicModel> result = null;
                
                try
                {
                    // If a task has been cancelled, the call to the 
                    // SqlExecutor.SelectProcedureAsync method throws an OperationCanceledException.
                    result = await _context.SqlExecutor.SelectProcedureAsync<DynamicModel>(
                                                                    procedureName,
                                                                    new object[] { idParam, nameParam },
                                                                    cts.Token);
                            
                    Console.WriteLine("DepartmentName: {0}", nameParam.Value);
                }
                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