SqlBuilder.Parameter<TType>(string name = "") Method
.NET Standard 2.x | Current Version (1.0.1)
Creates an ISqlParameter
object which represents a SQL parameter and specifies the data type of the parameter value by the Type parameter.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public static ISqlParameter Parameter<TType>(string name = "");
Type Parameters
TType
The type of the parameter value.
Parameters
name
System.String
The name of the SQL parameter.
The default value of this parameter is String.Empty
, which is unnamed.
If you use the unnamed parameter in a ISqlBuilder
object, they will be specified to @p0
, @p1
, ..., and @pn
by the adding sequence of the parameters.
Returns
SnapObjects.Data.ISqlParameter
Returns the newly created SQL parameter.
Remarks
When building with a raw SQL, 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, for example, uses @parametername
for SQL Server and uses :parametername
for Oracle.
Examples
The following code example demonstrates how to use the Parameter method to define a parameter and specify the parameter in the WHERE condition. The parameter value is passed in dynamically.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.SqlBuilderExamples
{
public class ParameterExample
{
private SchoolContext _context;
public ParameterExample(SchoolContext dataContext)
{
// Sets Data Context.
_context = dataContext;
}
public void Example1()
{
var sqlQueryBuilder = new SqlQueryBuilder();
// Defines a parameter named "id" of "int" type.
var paramId = SqlBuilder.Parameter<int>("id");
// Creates a SQL statement.
var query = sqlQueryBuilder
.Select("*")
.From("Department")
.Where("DepartmentID", paramId);
// Converts to raw SQL for the database corresponding to the data context.
string sql = query.ToSqlString(_context);
Console.WriteLine(sql);
/*This code example produces the following output:
SELECT
*
FROM [Department]
WHERE ([DepartmentID] = @id)
*/
}
}
}
Applies to
.NET Standard
2.x