public/New-GzSqlClientParameter.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71


function New-GzSqlClientParameter() {
<#
    .SYNOPSIS
    Creates a new System.Data.SqlClient.SqlParameter object.
 
    .DESCRIPTION
    By default, the .NET framework has an implementation for SqlClient and
    thus this cmdlet exists to help create new SqlParameter objects.
 
    .PARAMETER ParameterName
    The name of the parameter. The `Name` Parameter is an alias for this one.
 
    .PARAMETER DbType
    The `System.Data.SqlClient.SqlDbType` type for this SQL parameter.
 
    .PARAMETER Value
    The value that will be bound to this parameter.
 
    .PARAMETER Size
    (Optional). Sets the maximum size in bytes for this parameter.
 
    .PARAMETER Scale
    (Optional). Sets the number of decimal places to which the `Value` is resolved
 
    .PARAMETER Precision
    (Optional). Sets the maximum number of digits used to represent the Value.
 
    .EXAMPLE
     $parameter = New-GzSqlClientParameter "FirstName" "NVarChar" "Nerdy" -Limit 255
 
     .LINK
     https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter(v=vs.110).aspx
 
     .LINK
     https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.sqldbtype(v=vs.110).aspx
#>

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, Position = 1)]
        [Alias("Name")]
        [string] $ParameterName,
        [Parameter(Mandatory = $true, Position = 2)]
        [System.Data.SqlClient.SqlDbType] $DbType,
        [Parameter(Mandatory = $true, Position = 3)]
        [object] $Value,
        [Nullable[int]] $Size = $null,
        [Nullable[byte]] $Precision = $null,
        [Nullable[int]] $Scale = $null 
    )
    [System.Data.SqlClient.SqlDbType]::
    $parameter = New-Object System.Data.SqlClient.SqlParameter 
    $parameter.ParameterName = "@$ParameterName";
    $parameter.SqlDbType = $DbType;
    $parameter.Value = $Value;

    if($Size.HasValue) {
        $parameter.Size = $Size.Value;
    }

    if($Precision.HasValue) {
        $parameter.Size = $Precision.Value;
    }

    if($Scale.HasValue) {
        $parameter.Size = $Scale.Value;
    }

    return $parameter;
}