Show / Hide Table of Contents

    ISqlExecutor.Execute(string sqlText, params object[] parameters) Method

    .NET Standard 2.x

    Executes a SQL statement which does not need to return any data. It is used to execute the SQL statements like UPDATE, INSERT, DELETE etc.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public int Execute(string sqlText, 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.

    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

    System.Int32

    Returns the number of the affected rows if succeeds.

    Remarks

    You can use parameter placeholders in the raw SQL Statement. It is recommended to use the syntax for parameter placeholders that is specific to the data source. e.g., uses @parametername for SQL Server and uses :parametername for Oracle.

    Examples

    The following code example demonstrates how to use the Execute method to execute the SQL statement to insert, delete and update data.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlExecutorExamples
    {
        public class ExecuteExample
        {
            private readonly SchoolContext _context;
    
            public ExecuteExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                // Inserts a new course record, uses the parameter placeholders in the 
                // raw SQL. 
                string sql = @"INSERT Course(CourseID, Title, Credits, DepartmentID)
                            VALUES (@id, @title, @credits, @departmentId)";
    
                // Executes the SQL INSERT statement.
                // Passes the arguments directly in the order in which each parameter 
                // identifier name appear in `sqlText` for the first time.
                int row = _context.SqlExecutor.Execute(sql, 1, "New Course", 3, 7);
    
                Console.WriteLine("{0} records have been inserted into the database.",
                                  row);
    
    
                // Deletes a course record.
                sql = @"DELETE FROM Course 
                        WHERE CourseID = @id";
    
                // Passes 1 as the argument.
                row = _context.SqlExecutor.Execute(sql, 1);
    
                Console.WriteLine("{0} records have been deleted from the database.",
                                  row);
    
    
                // Updates the title for the Course whose ID is 1045.
                sql = @"UPDATE Course SET Title = 'New Course'
                        WHERE CourseID = 1045";
    
                // Executes the SQL UPDATE statement.
                row = _context.SqlExecutor.Execute(sql);
    
                Console.WriteLine("{0} records have been updated into the database.",
                                  row);
    
                /*This code produces the following output:
    
                1 records have been inserted into the database.
                1 records have been deleted from the database.
                1 records have been updated into the database.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon