ISqlBuilder.GetParameterPlacehold(string name) Method
.NET Standard 2.x
Gets the placeholder string for a SQL parameter.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
string GetParameterPlacehold(string name);
Parameters
name
System.String
The name of the SQL parameter to get the placeholder string.
Returns
System.String
Returns the placeholder string for the SQL parameter if the parameter name exists in the current object.
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.
You can also use the return value of ISqlBuilder.GetParameterPlaceHold(parametername)
method as a parameter placeholder in the raw SQL statement.
Examples
The following code example demonstrates how to use the GetParameterPlacehold
method.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlBuilderExamples
{
public class GetParameterPlaceholdExample
{
private SchoolContext _context;
public GetParameterPlaceholdExample(SchoolContext dataContext)
{
// Sets Data Context.
_context = dataContext;
}
public void Example()
{
// Builds a SQL statement.
var sqlBuilder = new SqlQueryBuilder();
// Adds two parameters.
sqlBuilder.AddParameters(
SqlBuilder.Parameter<int>("deptId"),
SqlBuilder.Parameter<string>("deptName"));
// Gets the placeholder for these two paramters.
string deptIdPlaceHolder =
sqlBuilder.GetParameterPlacehold("deptId");
Console.WriteLine("PlaceHolder for the deptId parameter: {0}",
deptIdPlaceHolder);
string deptNamePlaceHolder =
sqlBuilder.GetParameterPlacehold("deptName");
Console.WriteLine();
Console.WriteLine("PlaceHolder for the deptName parameter: {0}",
deptIdPlaceHolder);
// Uses placeholders when building a SQL statement.
// It is the same as using @deptid or :deptid.
sqlBuilder.Select("name")
.From("Department")
.Where("DepartmentId", deptIdPlaceHolder)
.OrWhere("name", deptNamePlaceHolder);
Console.WriteLine();
Console.WriteLine("The SQL SELECT statement: \n{0}",
sqlBuilder.ToSqlString(_context));
// Gets the first "name" column in the result set.
var scalar =
_context.SqlExecutor.Scalar<string>(sqlBuilder, 4, "Economics");
Console.WriteLine();
Console.WriteLine("The department name: {0}", scalar);
/*This code produces the following output:
PlaceHolder for the deptId parameter:
__P_P_H_S__prefix___name_deptId__P_P_H_E__
PlaceHolder for the deptName parameter:
__P_P_H_S__prefix___name_deptId__P_P_H_E__
The SQL SELECT statement:
SELECT
[name]
FROM [Department]
WHERE ([DepartmentId] = @deptId
OR [name] = @deptName)
The department name: Economics
*/
}
}
}
Applies to
.NET Standard
2.x