Database

Parameter Definitions

The parameter definition methods (WithIntegerParameter, WithVarCharParameter, WithDecimalParameter, and so on) declare the type of each parameter the SQL refers to by @name. Each method takes the parameter name without the @ and returns the database object for chaining. WithParameterLength sets the maximum length for character parameters. WithParameterPrecision and WithParameterScale set precision and scale for decimal and numeric parameters. Supported types are Integer, Decimal, Float, Money, Char, NChar, VarChar, NVarChar, Text, NText, Datetime, Time, Timestamp, and Image. WithTableParameter declares a table-valued parameter, and WithUDTTName names the user defined table type it maps to. The declared parameters line up in order with the columns of the grid you pass to Using, which is how one chain writes many rows. There is no boolean or bit type. The recommended pattern is a nullable Datetime, where NULL means false and a set date means true. WithCharParameter, WithNCharParameter, WithTextParameter and WithNTextParameter define fixed and variable length text parameters. WithFloatParameter, WithMoneyParameter, WithTimeParameter and WithTimestampParameter define the numeric and temporal types, and WithImageParameter a binary one.

Examples

GPAL Fluent: High-level fluent C# API

//Column definition methods are chained in the order the columns should appear in the schema. WithParameterLength, WithParameterPrecision, and WithParameterScale apply to the most recently defined column.

// Declare the parameters an insert refers to, then write a grid of rows through it

GPAL.Database

.WithConnectionString(connStr)

.WithCreateAs("INSERT INTO Customers (Name, Email, Balance) VALUES (@Name, @Email, @Balance)")

.ToGPALObject()

.Create

.WithNVarCharParameter("Name")

.WithParameterLength(100)

.WithNVarCharParameter("Email")

.WithParameterLength(255)

.WithDecimalParameter("Balance")

.WithParameterPrecision(18)

.WithParameterScale(2)

.Using(customersGrid)

.Execute(out int rowCount);


// A table-valued parameter sends the whole grid as one argument

GPAL.Database

.WithConnectionString(connStr)

.WithSQLCommand("EXEC ImportCustomers @Rows")

.WithTableParameter("Rows")

.WithUDTTName("dbo.CustomerImport")

.Using(customersGrid)

.Execute(out rowCount);

💬 Ask GPAL