SqlContext Class
.NET Standard 2.x
Stores the dynamic SQL statement and the parameter in use.
Namespace: PowerScript.Bridge
Assembly: PowerScript.Bridge.dll
Syntax
public class SqlContext;
Properties
Name | Return Type | Description |
---|---|---|
Parms | SnapObjects.Data.ParamValue | Gets the parameter list of SqlContext. |
SqlText | string | Gets the SQL text stored in SqlContext. |
Methods
Name | Return Type | Description |
---|---|---|
SetParm(int index, object value) | void | Specifies a value for an input parameter in the SqlContext. |
SetParm<TValue>(int index, TValue value) | void | Specifies a value for an input parameter in the SqlContext. |
Examples
The following code example demonstrates how to use the SqlContext
object to specify a SQL SELECT statement and define the parameters, and then use the SelectOne
method to execute this SQL statement.
using System;
using PowerScript.Bridge;
namespace Appeon.ApiDoc.SqlContextExamples
{
public class SqlContextExample
{
private readonly SchoolContext _context;
public SqlContextExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example()
{
// Defines the SQL SELECT statement.
var sql = @"select EnrollmentID, Grade from StudentGrade " +
"where StudentID = @StudentID and CourseID=@CourseID";
// Creates the SqlContext object
var sqlContext = _context.CreateSqlContext(sql);
// Defines the parameter for the SQL statement
sqlContext.SetParm(0, 2);
sqlContext.SetParm(1, 2021);
// Calls the SelectOne method to execute the SQL statement and the parameters defined in SqlContext.
var result = _context.SqlExecutor.SelectOne(sqlContext.SqlText,
out SqlResult resultSet, sqlContext.Parms);
// Gets the value of the EnrollmentID column.
int enrollmentID = result.GetValue<int>("EnrollmentID");
// Gets the value of the Grade column.
decimal grade = result.GetValue<decimal>("Grade");
Console.WriteLine($"EnrollmentID = {enrollmentID}, Grade = {grade}.");
/*This code produces the following output:
EnrollmentID = 1, Grade = 4.00.
*/
}
}
}