Show / Hide Table of Contents

    SqlExecutorExtensions.SelectOne(string sqlText, out SqlResult resultSet, params object[] parameters) Method

    .NET Standard 2.x

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

    Namespace: PowerScript.Bridge

    Assembly: PowerScript.Bridge.dll

    Syntax

    public static DynamicModel SelectOne(this ISqlExecutor sqlExecutor, string sqlText, out SqlResult resultSet, params object[] parameters)
    

    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.

    resultSet PowerScript.Bridge.SqlResult

    The result of the execution.

    parameters System.Object[]

    (Optional) One or more ParamValue 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 ParamValue object.

    Returns

    SnapObjects.Data.DynamicModel

    Returns a TModel object which represents the first row of the result set. Returns null if there are no rows in the result set.

    Examples

    The following code example demonstrates how to execute the SQL statement and put the first row of the result set to a DynamicModel object.

    using System;
    using PowerScript.Bridge;
    
    namespace Appeon.ApiDoc.ISqlExecutorExtensionsExamples
    {
        public class SelectOneExample
        {
            private readonly SchoolContext _context;
    
            public SelectOneExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example()
            {
                // Defines the SQL SELECT statement.
                var sql = @"select CourseID, Title from Course where CourseID < @CourseID";
                // Defines the parameters for the SQL statement.
                int courseID = 2000;
    
                // Executes the SQL statement and gets the first row of the result set.
                // The execution result of SQL statement will be stored in the SqlResult object.
                var result = _context.SqlExecutor.SelectOne(sql, out SqlResult resultSet, courseID);
    
                // Gets the value of the Title column.
                string title = result.GetValue<string>("Title");
                // Gets the SQLCode.
                int sqlCode = resultSet.SqlCode;
    
                Console.WriteLine($"Title = {title}, SqlResult SqlCode = {sqlCode}.");
    
                /*This code produces the following output:
    
                Title = Calculus, SqlResult SqlCode = 0.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon