Show / Hide Table of Contents

    ISqlExecutor.SelectProcedureToStore<TModel>(this ISqlExecutor sqlExecutor, string sqlText, params object[] parameters) Method

    .NET Standard 2.x

    (Obsolete) Executes a SQL stored procedure and returns an IModelStore<TModel> object which contains the result set.

    Namespace: DWNet.Data

    Assembly: DWNet.Data.dll

    Syntax

      public static IModelStore<TModel> SelectProcedureToStore<TModel>(this ISqlExecutor sqlExecutor, string sqlText, params object[] parameters)
    

    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

    sqlExecutor SnapObjects.Data.ISqlExecutor

    An ISqlExecutor that can run SQL statements.

    sqlText 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

    SnapObjects.Data.IModelStore<TModel>

    Returns an IModelStore<TModel> object that contains the result set.

    Examples

    The following code example demonstrates how to execute the specified procedure in the database and return the query result to a ModelStore object.

    using DWNet.Data;
    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.SqlExecutorExtensionsExamples
    {
        public class SelectProcedureToStoreExample
        {
            private readonly SchoolContext _context;
    
            public SelectProcedureToStoreExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example()
            {
                // Specifies the procedure to be executed.
                var proc = "GetStudentGrades";
    
                /* StoredProcedure GetStudentGrades:
                
                CREATE PROCEDURE [dbo].[GetStudentGrades]
                @StudentID int
                AS
                SELECT EnrollmentID, Grade, CourseID, StudentID FROM dbo.StudentGrade
                WHERE StudentID = @StudentID
    
                GO
                */
    
                // Sets value for the @StudentID parameter.
                var studentId = ParamValue.New<int>("StudentID", 7);
    
                // Executes the SQL statement and returns a ModelStore.
                var ms = _context.SqlExecutor
                    .SelectProcedureToStore<DynamicModel>(proc, studentId);
    
                Console.WriteLine("Count: {0}.", ms.Count);
                Console.WriteLine();
    
                foreach (var model in ms)
                {
                    Console.WriteLine("Student ID: {0}.",
                       model.GetValue<int>("StudentID"));
    
                    Console.WriteLine("Course ID: {0}.",
                        model.GetValue<int>("CourseID"));
    
                    Console.WriteLine("Grade: {0}.",
                        model.GetValue<decimal>("Grade"));
    
                    Console.WriteLine();
                }
    
                /*This code produces the following output:
    
                Count: 2.
    
                Student ID: 7.
                Course ID: 2021.
                Grade: 3.50.
    
                Student ID: 7.
                Course ID: 2042.
                Grade: 4.00.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon