ISqlExecutor.Scalar<TValue>(ISqlQueryBuilder sqlQueryBuilder, params object[] parameters) Method
.NET Standard 2.x
Executes the SQL SELECT statement created by an ISqlQueryBuilder
object and returns the first column of the first row in the result set. All other columns and rows are ignored.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public TValue Scalar<TValue>(ISqlQueryBuilder sqlQueryBuilder, params object[] parameters);
Type Parameters
TValue
The datatype of the first column in the result set.
Parameters
sqlQueryBuilder
SnapObjects.Data.ISqlQueryBuilder
A SqlQueryBuilder
object which can be used to generate a SQL SELECT statement.
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 they appear in the SQL SELECT statement generated by sqlQueryBuilder
without using the ParamValue object.
Returns
TValue
Returns the first column of the first row in the result set. Returns null
, if no data is found in the result set.
Examples
The following code example demonstrates how to use the Scalar<TValue>(ISqlQueryBuilder, params object[]
method to execute the SQL statement generated by an ISqlQueryBuilder
object.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlExecutorExamples
{
public class ScalarExample
{
private readonly SchoolContext _context;
public ScalarExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example2()
{
// Creates a SqlQueryBuilder.
var builder = new SqlQueryBuilder();
var idParam = SqlBuilder.Parameter<int>("id");
// Defines a parameter.
builder.AddParameters(idParam);
// Builds the SQL SELECT statement.
builder.Select("Title")
.From("Course")
.Where("CourseID", idParam);
// Executes the SQL statement and the first column of the first row
// in the result set.
int id = 2042;
var title = _context.SqlExecutor.Scalar<string>(builder, id);
Console.WriteLine("Title: {0} ", title);
/*This code produces the following output:
Title: Literature
*/
}
}
}
Applies to
.NET Standard
2.x