Show / Hide Table of Contents

    ISqlExecutor.ExecuteProcedureAsync(string procedureName, params object[] parameters) Method

    .NET Standard 2.x

    Asynchronously executes a SQL stored procedure which does not need to return the result set.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public Task<int> ExecuteProcedureAsync(string procedureName, params object[] parameters);
    

    Parameters

    procedureName System.String

    The name of the SQL stored procedure.

    parameters System.Object[]

    (Optional) The parameters for executing the SQL stored procedure.

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

    Returns

    Task<int>

    Returns a task that represents the asynchronous operation.

    Examples

    The following code example demonstrates how to use this method to asynchronously execute the specified procedure in the database to get the name of a department.

    using System;
    using System.Data;
    using SnapObjects.Data;
    using System.Data.SqlClient;
    using System.Threading.Tasks;
    using System.Threading;
    
    namespace Appeon.ApiDoc.ISqlExecutorExamples
    {
        public class ExecuteProcedureAsyncExample
        {
            private readonly SchoolContext _context;
            
            public ExecuteProcedureAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public async Task<int> Example1()
            {
                // Specifies the procedure to be executed.
                // It inserts a person record to database and returns the new PersonID.
                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");
                
                // Asynchronously Executes the SQL statement and gets the department by
                // nameParam.
                int rowAffected = await _context.SqlExecutor.ExecuteProcedureAsync(
                        procedureName, idParam, nameParam);
                    
                Console.WriteLine("Department Name: {0}", nameParam.Value);
                
                return rowAffected;
                
                /*This code produces the following output:
                
                    Department Name: Economics
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon