public/Add-DbParameters.ps1
function Add-DbParameters() { Param( [Parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true)] [System.Data.IDbCommand] $Command, [Parameter(Position = 2)] [Object] $Parameters, [Parameter(Position = 3)] [String] $ParameterPrefix, [switch] $Update ) $factory = Get-DbProviderFactory if($Parameters) { if([string]::IsNullOrWhiteSpace($ParameterPrefix)) { $ParameterPrefix = Get-DbParameterPrefix } if($Parameters -is [System.Management.Automation.PSCustomObject]) { if($Update.ToBool()) { $Parameters | Get-Member -MemberType NoteProperty | Foreach-Object { $Name = $_.Name $Name = "$($ParameterPrefix)$Name" $Value = $Parameters.$Name $Command.Parameters[$Name].Value = $Value } } else { $Parameters | Get-Member -MemberType NoteProperty | Foreach-Object { $Name = $_.Name $Value = $Parameters.$Name $p = $factory.CreateParameter(); $p.ParameterName = "$($ParameterPrefix)$Name" $p.Value = $Value $Command.Parameters.Add($p) | Out-Null } } } elseif($Parameters -is [hashtable]) { if($Update.ToBool()) { foreach($key in $Parameters.Keys) { $Value = $Parameters[$key] $Name = "$($ParameterPrefix)$key" $Command.Parameters[$Name].Value = $Value; } } else { foreach($key in $Parameters.Keys) { $Value = $Parameters[$key] $Name = "$($ParameterPrefix)$key" $p = $factory.CreateParameter() $p.ParameterName = $Name $p.Value = $Value $Command.Parameters.Add($p) | Out-Null } } } elseif($Parameters -is [Array]) { if($Update.ToBool()) { for($i = 0; $i -lt $Parameters.Length; $i++) { $Name = "$($ParameterPrefix)$i" $Value = $Parameters[$i] $p = $Command.Parameters[$Name].Value = $Value; } } else { for($i = 0; $i -lt $Parameters.Length; $i++) { $value = $Parameters[$i]; if($value -is [System.Data.IDbParameter]) { $Command.Parameters.Add($value) | Out-Null continue; } $p = $factory.CreateParameter() $p.ParameterName = "$($ParameterPrefix)$i" $p.Value = $Value $Command.Parameters.Add($p) | Out-Null } } } else { throw Exception "Parameters does not support type [$($Parameters.GetType())]" } } } |