< Back

ConvertTo-CTypeSqlWrapper

Mon Jan 13, 2020 8:18 am

NAME ConvertTo-CTypeSqlWrapper



SYNOPSIS

Wraps a SQL row in a wrapper object.





SYNTAX

ConvertTo-CTypeSqlWrapper [-InputObject <DataRow>] [-TypeName] <String> [-AddCType] [[-AddCTypeScriptBlock]

<ScriptBlock[]>] [<CommonParameters>]





DESCRIPTION

Wraps a SQL row in a wrapper object. The wrapper class must first be defined by

Add-CType -SqlWrapper. The wrapped object has several advantages over the

raw System.Data.DataRow object:

(1) Property value System.DBNull is automatically converted to $null so that it is boolean-false in scripting

(2) Type can be use in [OutputType([typename])] attributes

(3) Type can be assigned formatting directives and other PowerShell type decorations

(4) All properties are read-only

(5) Hides unneeded properties of base SQL object





PARAMETERS

-InputObject <DataRow>

System.Data.DataRow object to be wrapped



Required? false

Position? named

Default value

Accept pipeline input? true (ByValue)

Accept wildcard characters? false



-TypeName <String>

Name of wrapper class, should be the same as for Add-CType



Required? true

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-AddCType [<SwitchParameter>]

If specified, the type will be created automatically

the first time an input object of this type is received,

where the first object is a template.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-AddCTypeScriptBlock <ScriptBlock[]>

If specified, the type will be created automatically

the first time an input object of this type is received,

where the first object is a template and this script block

defines additional properties of the type. This implies -AddCType.

Note the limitations in Notes below.



Required? false

Position? 2

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





Note that the type will be defined based on the first InputObject encountered.

Even if subsequent objects have a different property list, the type cannot be changed.



Note that you can define a type using AddCTypeScriptBlock, but because that type

cannot be defined until the first template object is created, you may have difficulty

using the type in an OutputType([typename]) block or a parameter definition.

Consider generating the type definition with ConvertTo-CTypeDefinition

and pasting the CType definition directly into your code.



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>$connection = New-Object System.Data.SqlClient.SqlConnection $connectionString



$command = New-Object System.Data.SqlClient.SqlCommand $commandString,$connection

$adapter = New-Object System.Data.SqlClient.SqlDataAdapter $command

$dataset = New-Object System.Data.DataSet

$null = $adapter.Fill($dataSet)

$result = $tables[0].Rows

$wrappedResult = $result | ConvertTo-CTypeSqlWrapper -TypeName MyCompany.MyNamespace.MyWrapperClass











RELATED LINKS

Add-CType

CType

ConvertTo-CTypeSqlDefinition