SqlParameterAttribute Class
.NET Standard 2.x
Defines a parameter with name and type in the model class.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Inheritance Constructor
System.Attribute
SnapObjects.Data.ISqlParameter
Syntax
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
public class SqlParameterAttribute : Attribute, ISqlParameter
Constructors
Name | Description |
---|---|
SqlParameterAttribute(string name, Type dataType) | Initializes a new instance of the SqlParameterAttribute class. |
Properties
Name | Return Type | Description |
---|---|---|
DataType | Type | Gets the data type of the parameter. |
Direction | System.Data.ParameterDirection | Gets the direction type of the parameter. |
Name | string | Gets the name of the parameter. |
Remarks
The parameter can be referenced by its name in the SQL expression parameter for the other attributes in the model class. The format to make reference: :paramName
.
Examples
The following code example demonstrates how to use the SqlParameter
attribute. It applies the SqlParameter
attribute named "nameArgument" to the DepartmentByName class. Then you can use ":nameArgument" in any raw SQL expression parameter for the other attributes applied to the DepartmentByName class.
Model: DepartmentByName
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SnapObjects.Data;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.Department table.
/// It uses the primary key to generate the Where clause for the Update operation.
/// The SqlParameter attribute defines an argument: nameArgument.
/// The SqlWhere attribute adds a Where clause using the nameArgument argument.
///
/// DepartmentID is the primary key.
/// </summary>
[SqlParameter("nameArgument", typeof(string))]
[Table("Department", Schema = "dbo")]
[UpdateWhereStrategy(UpdateWhereStrategy.KeyColumns)]
[SqlWhere("Name = :nameArgument")]
public class DepartmentByName
{
[Key]
public int DepartmentID { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
public DateTime StartDate { get; set; }
public int? Administrator { get; set; }
}
}
Example Method:
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.SqlParameterAttributeExamples
{
public class SqlParameterAttributeExample
{
private SchoolContext _context;
public SqlParameterAttributeExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
// Gets the SQL statement.
string sql =ModelSqlBuilder.GetBuilder<DepartmentByName>(_context).QuerySql;
Console.WriteLine("SQL statement:");
Console.WriteLine(sql);
// Executes the SQL statement to query the English department.
var department = _context.SqlExecutor.Select<DepartmentByName>(sql, "English");
/* This code example produces the following output:
SQL statement:
SELECT
[DepartmentID],
[Name],
[Budget],
[StartDate],
[Administrator]
FROM [dbo].[Department]
WHERE ([Name] = @nameArgument)
*/
/* Internally generated T-SQL:
exec sp_executesql N'SELECT
[DepartmentID],
[Name],
[Budget],
[StartDate],
[Administrator]
FROM [dbo].[Department]
WHERE ([Name] = @nameArgument)',N'@nameArgument nvarchar(4000)',@nameArgument=N'English'
*/
}
}
}
Applies to
.NET Standard
2.x