ISqlExecutor.ScalarAsync<TValue>(string sqlText, params object[] parameters) Method
.NET Standard 2.x
Asynchronously executes the SQL SELECT statement 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 Task<TValue> ScalarAsync<TValue>(string sqlText, params object[] parameters);
Type Parameters
TValue
The datatype of the first column in the result set.
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 objects.
Returns
Task<TValue>
Returns a task that represents the asynchronous operation.
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 ScalarAsync(String, params Object[])
method to execute the SQL SELECT statement and return the first column of the first row in the result set.
using System;
using System.Threading;
using System.Threading.Tasks;
using SnapObjects.Data;
namespace Appeon.ApiDoc.ISqlExecutorExamples
{
public class ScalarAsyncExample
{
private readonly SchoolContext _context;
public ScalarAsyncExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public async void Example1()
{
// Define the SQL Statement.
string sql = "select Title from Course where Credits = 2";
// Asynchronously Excute the SQL Statement and returns the query result.
string title = await _context.SqlExecutor.ScalarAsync<string>(sql);
Console.WriteLine("Title = {0}", title);
/* This code produces the following output:
Course.Title = Poetry
*/
}
}
}
Applies to
.NET Standard
2.x