ISqlModelMapper.TrackSqlBuilder(ISqlInsertBuilder insertBuilder, params object[] parameters) Method
.NET Standard 2.x
Tracks a database table insert operation. A SQL INSERT statement will be generated using the specified ISqlInsertBuilder
object. When ISqlModelMapper.SaveChanges method is called, the SQL INSERT statement will be executed.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public ISqlModelMapper TrackSqlBuilder(ISqlInsertBuilder insertBuilder, params object[] parameters);
Parameters
insertBuilder
SnapObjects.Data.ISqlInsertBuilder
An ISqlInsertBuilder
object used to build the SQL INSERT statement.
parameters
System.Object[]
(Optional) One or more values that you want to use as arguments in the SQL INSERT statement defined in insertBuilder
.
Returns
SnapObjects.Data.ISqlModelMapper
Returns the current ISqlModelMapper
object, which can be used for executing other methods.
Examples
The following code example demonstrates how to add a student.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlModelMapperExamples
{
public class TrackSqlBuilderExample
{
private SchoolContext _context;
public TrackSqlBuilderExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example1()
{
var insertBuilder = new SqlInsertBuilder();
// Builds a SQL Insert statement with no parameters to register a new student.
insertBuilder.Insert("Person")
.ColumnValue("LastName", "Senior")
.ColumnValue("FirstName", "Letitia")
.ColumnValue("EnrollmentDate", new DateTime(2019, 1, 1))
.ColumnValue("Discriminator", "Student");
Console.WriteLine("SQL is:");
Console.WriteLine(insertBuilder.ToSqlString(_context));
// Tracks the SQL Insert statement and executes it in the database.
var dbResult = _context.SqlModelMapper.TrackSqlBuilder(insertBuilder)
.SaveChanges();
Console.WriteLine();
Console.WriteLine("The count of the inserted record is {0}.",
dbResult.InsertedCount);
Console.WriteLine();
// Builds a SQL Insert statement with parameters.
insertBuilder.Reset();
// Defines the parameters.
var paramLastName = SqlBuilder.Parameter<string>("lastName");
var paramFirstName = SqlBuilder.Parameter<string>("firstName");
var paramEnrollmentDate =
SqlBuilder.Parameter<DateTime>("enrollmentDate");
// Adds parameters to the insertBuilder.
insertBuilder.AddParameters(paramLastName,
paramFirstName, paramEnrollmentDate);
// Builds the SQL Insert statement.
insertBuilder.Insert("Person")
.Column("LastName", paramLastName)
.Column("FirstName", paramFirstName)
.Column("EnrollmentDate", paramEnrollmentDate)
.ColumnValue("Discriminator", "Student");
Console.WriteLine("SQL with parameters is:");
Console.WriteLine(insertBuilder.ToSqlString(_context));
// Tracks the SQL Insert statement and executes it in the database.
dbResult = _context.SqlModelMapper
.TrackSqlBuilder(insertBuilder,
"Senior",
"Letitia",
new DateTime(2019, 1, 1))
.SaveChanges();
Console.WriteLine();
Console.WriteLine("The count of the inserted record is {0}.",
dbResult.InsertedCount);
/* The code produces the following output:
SQL is:
INSERT INTO Person([LastName],
[FirstName],
[EnrollmentDate],
[Discriminator]) VALUES(N'Senior',
N'Letitia',
'2019-01-01 00:00:00.000',
N'Student')
The count of the inserted record is 1.
SQL with parameters is:
INSERT INTO Person([LastName],
[FirstName],
[EnrollmentDate],
[Discriminator]) VALUES(@lastName,
@firstName,
@enrollmentDate,
N'Student')
The count of the inserted record is 1.
*/
}
}
}
Applies to
.NET Standard
2.x