ISqlInsertBuilder.Column(string columnName, ISqlParameter parameter) Method
.NET Standard 2.x
Inserts dynamic data into the column of a table. The dynamic data will be specified by a SQL parameter when the SQL is executed.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
ISqlInsertBuilder Column(string columnName, ISqlParameter parameter);
Parameters
columnName
System.String
The name of the column into which data will be inserted.
parameter
SnapObjects.Data.ISqlParameter
An ISqlParameter
object which represents a SQL parameter.
It can be created by calling the SqlBuilder.Parameter method.
Returns
SnapObjects.Data.ISqlInsertBuilder
Returns the current ISqlInsertBuilder
object.
Examples
The following code example demonstrates how to use the Column(string columnName, ISqlParam parameter) method to insert dynamic data to the row. The dynamic data will be specified by a SQL parameter when the SQL is executed.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlInsertBuilderExamples
{
public class ColumnExample
{
private SchoolContext _context;
public ColumnExample(SchoolContext dataContext)
{
// Sets Data Context.
_context = dataContext;
}
public void Example()
{
// Declares SqlInsertBuilder.
var sqlbuilder = new SqlInsertBuilder();
// Creates a SQL INSERT statement and inserts data into the "Department" table.
// The data to be inserted is dynamic and will be specified when SQL is executed.
sqlbuilder.Insert("Department")
.Column("DepartmentID", SqlBuilder.Parameter<int>("Id"))
.Column("Name", SqlBuilder.Parameter<string>("Name"))
.Column("Budget", SqlBuilder.Parameter<decimal>("Budget"))
.Column("StartDate", SqlBuilder.Parameter<DateTime>("curdate"));
// Converts to raw SQL for the database corresponding to the data context.
string sql = sqlbuilder.ToSqlString(_context);
Console.WriteLine(sql);
// Executes the SQL with the arguments passed in.
var departments = _context.SqlExecutor
.Execute(sql, 20, "Chinese", 18000m, DateTime.Now);
/*This code produces the following output:
INSERT INTO Department([DepartmentID],
[Name],
[Budget],
[StartDate]) VALUES(@Id,
@Name,
@Budget,
@curdate)
*/
}
}
}
Applies to
.NET Standard
2.x