DataContextExtensions.CreateSqlContext(string sqlText) Method
.NET Standard 2.x
Creates the SqlContext
object according to the SQL statement.
Namespace: PowerScript.Bridge
Assembly: PowerScript.Bridge.dll
Syntax
public static SqlContext CreateSqlContext(this DataContext context, string sqlText)
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.
Returns
Returns a SqlContext
object.
Examples
The following code example demonstrates how to use the CreateSqlContext
method to define a SQL SELECT statement and the parameters, and then use the SqlExecutor
object to execute this SQL statement.
using System;
using SnapObjects.Data;
using PowerScript.Bridge;
namespace Appeon.ApiDoc.DataContextExtensionsExamples
{
public class CreateSqlContextExample
{
private readonly SchoolContext _context;
public CreateSqlContextExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example()
{
// Defines the SQL Select statement.
string sql = "select DepartmentID, Name from Department where DepartmentID <@id";
// Creates the SqlContext object according to the SQL statement.
var sqlContext = _context.CreateSqlContext(sql);
// Sets the parameters for the SqlContext object.
sqlContext.SetParm(0, 3);
// Uses the SqlExecutor object to execute this SQL statement.
var models = _context.SqlExecutor.Select<DynamicModel>(sqlContext.SqlText, sqlContext.Parms);
// Shows the total number of models.
Console.WriteLine($"models Count = {models.Count}");
/*This code produces the following output:
models Count = 2
*/
}
}
}
Applies to
.NET Standard
2.x