ISqlExecutor.ExecuteProcedure(string procedureName, params object[] parameters) Method
.NET Standard 2.x
Executes a SQL stored procedure which does not need to return the result set.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public int ExecuteProcedure(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
Returns the number of the affected rows if succeeds.
Examples
The following code example demonstrates how to use the ExecuteProcedure
method to execute the specified procedure in the database to get the name of a department.
using SnapObjects.Data;
using System;
using System.Data;
using System.Data.SqlClient;
namespace Appeon.ApiDoc.ISqlExecutorExamples
{
public class ExecuteProcedureExample
{
private readonly SchoolContext _context;
public ExecuteProcedureExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example()
{
// Specifies the procedure to be executed.
// It inserts a person record to database and returns the new PersonID.
var 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.
var idParam = ParamValue.New<string>("id", "4");
// The parameter 'Name' is Output type.
var nameParam = ParamValue.Output<string>("name");
// Executes the SQL statement and gets the department by nameParam.
_context.SqlExecutor.ExecuteProcedure(
procedureName, idParam, nameParam);
Console.WriteLine("Department Name: {0}", nameParam.Value);
/*This code produces the following output:
Department Name: Economics
*/
}
}
}
Applies to
.NET Standard
2.x