Created
May 3, 2019 11:32
-
-
Save pimbrouwers/2b48b7423bf6c8047340fa99f887bd3c to your computer and use it in GitHub Desktop.
PowerShell ADO.NET Wrapper (incomplete)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Open connection to SQL Server | |
function Open-SqlConnection { | |
param ( | |
[Parameter(Mandatory)][string] $Server, | |
[string] $Database, | |
[string] $User, | |
[string] $Password) | |
$connection = New-Object System.Data.SqlClient.SqlConnection | |
if ($Database -ne '') { | |
$databaseClause = "Database=$Database;" | |
} | |
if ($User -ne '' -and $Password -ne '') { | |
$connectionString = "Server=$Server;$databaseClause;User Id=$User;Password=$Password" | |
} | |
else { | |
$connectionString = "Server=$Server;$databaseClause;Trusted_Connection=true" | |
} | |
Write-Debug "`n------ [Open-SqlConnection]`n$connectionString`n------`n" | |
$connection.ConnectionString = $connectionString | |
$connection.Open() | |
$connection | |
} | |
# Close IDbConnection | |
function Close-IDbConnection { | |
param ([Parameter(Mandatory)][System.Data.IDbConnection] $Connection) | |
$Connection.Close() | |
$Connection.Dispose() | |
} | |
# IDbConnection.NewCommand() | |
function New-IDbCommand { | |
param ([Parameter(Mandatory)][string] $Query, | |
[hashtable] $Parameters = @{ }, | |
[Parameter(Mandatory)][System.Data.IDbConnection] $Connection) | |
$command = $Connection.CreateCommand() | |
$command.CommandText = $Query | |
Write-Debug "`n------ [New-IDbCommand]`n$Query`n------`n" | |
foreach ($p in $Parameters.GetEnumerator()) { | |
[void]$command.Parameters.AddWithValue($p.Name, $p.Value) | |
} | |
$command | |
} | |
# IDbCommand.ExecuteNonQuery() | |
function Invoke-IDbCommandNonQuery { | |
param ( | |
[Parameter(Mandatory)][System.Data.IDbConnection] $Connection, | |
[string] $Query, | |
[string] $InputFile, | |
[hashtable] $Parameters = @{ }) | |
if ($InputFile -ne '') { | |
$Query = [System.IO.File]::ReadAllText($InputFile) | |
} | |
if ($Query -ne '') { | |
$command = New-IDbCommand $Query $Parameters $Connection | |
[void]$command.ExecuteNonQuery() | |
} | |
} | |
# IDbCommand.ExecuteReader() | |
function Open-IDbCommandReader { | |
param ( | |
[Parameter(Mandatory)][System.Data.IDbConnection] $Connection, | |
[Parameter(Mandatory)][string] $Query, | |
[hashtable] $Parameters = @{ }) | |
$command = New-IDbCommand $Query $Parameters $Connection | |
, $command.ExecuteReader() # "," tells PS not to unroll collection | |
} | |
# IDataReader.Close() & IDataReader.Dispose() | |
function Close-IDataReader { | |
param ( | |
[Parameter(Mandatory)][System.Data.IDataReader]$Reader) | |
if ($Reader) { | |
$Reader.Close() | |
$Reader.Dispose() | |
} | |
} | |
# New SqlBulkCopy | |
function New-SqlBulkCopy { | |
param([Parameter(Mandatory)][System.Data.IDbConnection]$Connection) | |
New-Object System.Data.SqlClient.SqlBulkCopy($Connection) | |
} | |
# SqlBulkCopy.WriteToServer() via IDataReader | |
function Write-SqlBulkCopy { | |
param ( | |
[Parameter(Mandatory)][System.Data.SqlClient.SqlBulkCopy]$BulkCopy, | |
[Parameter(Mandatory)][System.Data.IDataReader]$Reader, | |
[string] $TableName, | |
[int] $BatchSize) | |
if ($TableName) { $BulkCopy.DestinationTableName = $TableName } | |
if ($BatchSize) { $BulkCopy.BatchSize = $BatchSize } | |
Write-Debug "`n------ [Write-SqlBulkCopy]`n$($BulkCopy.DestinationTableName) / Batch Size: $($BulkCopy.BatchSize)`n------`n" | |
$BulkCopy.WriteToServer($Reader) | |
} | |
# SqlBulkCopy.Close() & SqlBulkCopy.Dispose() | |
function Close-SqlBulkCopy { | |
param ([Parameter(Mandatory)][System.Data.SqlClient.SqlBulkCopy]$BulkCopy) | |
$BulkCopy.Close() | |
$BulkCopy.Dispose() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment