Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move DBnull scrubbing from C# type to PoweShell #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 12 additions & 41 deletions Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -361,50 +361,21 @@ function Invoke-Sqlcmd2 {

if ($As -eq "PSObject") {
#This code scrubs DBNulls. Props to Dave Wyatt
$cSharp = @'
using System;
using System.Data;
using System.Management.Automation;

public class DBNullScrubber
{
public static PSObject DataRowToPSObject(DataRow row)
{
PSObject psObject = new PSObject();

if (row != null && (row.RowState & DataRowState.Detached) != DataRowState.Detached)
{
foreach (DataColumn column in row.Table.Columns)
{
Object value = null;
if (!row.IsNull(column))
{
value = row[column];
}

psObject.Properties.Add(new PSNoteProperty(column.ColumnName, value));
}
function DataRowToPSObject ($row)
{
$PSObject = New-Object -TypeName psobject

if($null -ne $row -and ($row.RowState -band [System.Data.DataRowState]::Detached) -ne [System.Data.DataRowState]::Detached){
foreach ($column in $row.Table.Columns) {
$value = $null
if(!$row.IsNull($column)){
$value = $row[$column]
}

return psObject;
$PSObject | Add-Member -MemberType NoteProperty -Name $column.ColumnName -Value $value
}
}
'@

try {
if ($PSEdition -ne 'Core'){
Add-Type -TypeDefinition $cSharp -ReferencedAssemblies 'System.Data', 'System.Xml' -ErrorAction stop
} else {
Add-Type $cSharp -ErrorAction stop
}


}
catch {
if (-not $_.ToString() -like "*The type name 'DBNullScrubber' already exists*") {
Write-Warning "Could not load DBNullScrubber. Defaulting to DataRow output: $_."
$As = "Datarow"
}
$PSObject
}
}

Expand Down Expand Up @@ -627,7 +598,7 @@ function Invoke-Sqlcmd2 {
#Scrub DBNulls - Provides convenient results you can use comparisons with
#Introduces overhead (e.g. ~2000 rows w/ ~80 columns went from .15 Seconds to .65 Seconds - depending on your data could be much more!)
foreach ($row in $ds.Tables[0].Rows) {
[DBNullScrubber]::DataRowToPSObject($row)
DataRowToPSObject($row)
}
}
}
Expand Down