Show / Hide Table of Contents

    ISqlRawBuilder.Raw(string sqlText, params ISqlParameter[] parameters) Method

    .NET Standard 2.x

    Creates a SQL statement by the raw SQL and SQL parameters.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

     ISqlBuilder Raw(string sqlText, params ISqlParameter[] parameters);
    

    Parameters

    sqlText System.String

    A string of raw SQL.

    parameters SnapObjects.Data.ISqlParameter[]

    (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.

    Returns

    SnapObjects.Data.ISqlBuilder

    Returns an ISqlBuilder object which represents the current object.

    Examples

    The following code example demonstrates how to use the Raw(string sqlText, params ISqlParam[] parameters) method to define a SQL statement in a raw SQL string and add parameters to the SQL statement. The parameter in the raw SQL string starts with the "@" symbol.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlRawBuilderExamples
    {
        public class RawExample
        {
            private SchoolContext _context;
    
            public RawExample(SchoolContext dataContext)
            {
                // Sets Data Context.
                _context = dataContext;
            }
    
            public void Example()
            {
                // Creates a SqlRawBuilder object.
                var sqlbuilder = new SqlRawBuilder();
    
                // Retrieves the records from the "Department" table which satisfy this condition: 
                // DepartmentID smaller than the value of parameter a, and budget smaller than the value of parameter b. 
                sqlbuilder.Raw("select * from Department where " +
                    " DepartmentID < @a and budget < @b",
                    SqlBuilder.Parameter<int>("a"),
                    SqlBuilder.Parameter<decimal>("b"));
    
                // Converts to raw SQL for the database corresponding to the data context.
                string sql = sqlbuilder.ToSqlString(_context);
    
                Console.WriteLine(sql);
    
                // Executes the SQL statement and gets the total number of the records that satisfy the condition.
                var values = _context.SqlExecutor.Select<DynamicModel>(sql, 7, 300000).Count;
    
                Console.WriteLine("Number of records that satisfy the condition: {0}",
                    values);
    
                /*This code produces the following output:
                
                SELECT
                *
                FROM [Department]
                WHERE [DepartmentID] < @a
                AND [budget] < @b
                Number of records that satisfy the condition: 2
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon