Functions/GenXdev.Data.SqlServer/Get-SQLServerTransaction.ps1
|
# don't remove this line [dontrefactor] ############################################################################### <# .SYNOPSIS Creates and returns a SQL Server transaction object for batch operations. .DESCRIPTION Creates a SQL Server database connection and transaction object that can be used for batch operations. The caller is responsible for committing or rolling back the transaction. Requires an existing SQL Server database and connection. .LICENSE Copyright (C) 2026 René Vaessen / GenXdev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. .PARAMETER ConnectionString The SQL Server connection string for database access. .PARAMETER DatabaseName The name of the SQL Server database. Used with Server parameter to create connection string. .PARAMETER Server The SQL Server instance name. Defaults to 'localhost'. .PARAMETER IsolationLevel Transaction isolation level. Defaults to ReadCommitted. .PARAMETER CreateDatabaseIfNotExists Whether to create the database file if it doesn't exist. Defaults to true. .PARAMETER AutoConsent Force a consent prompt even if a preference is already set for SQL Server package installation, overriding any saved consent preferences. .PARAMETER AutoConsentAllPackages Automatically consent to third-party software installation and set a persistent preference flag for SQL Server package, bypassing interactive consent prompts. .EXAMPLE $transaction = Get-SQLServerTransaction -Server "localhost" -DatabaseName "MyDatabase" try { Invoke-SQLServerQuery -Transaction $transaction -Queries "INSERT INTO Users..." Invoke-SQLServerQuery -Transaction $transaction -Queries "UPDATE Users..." $transaction.Commit() } catch { $transaction.Rollback() throw } finally { $transaction.Connection.Close() } .EXAMPLE $transaction = Get-SQLServerTransaction -ConnectionString "Server=localhost;Database=MyDB;Integrated Security=true" .EXAMPLE $transaction = Get-SQLServerTransaction -Server "localhost" -DatabaseName "MyDatabase" -AutoConsentAllPackages #> function Get-SQLServerTransaction { [CmdletBinding(DefaultParameterSetName = 'DatabaseName')] [Alias('getsqltx', 'newsqltx')] param ( ########################################################################### [Parameter( Position = 0, Mandatory, ParameterSetName = 'ConnectionString', HelpMessage = 'The connection string to the SQL Server database.' )] [string]$ConnectionString, ########################################################################### [Parameter( Position = 0, Mandatory, ParameterSetName = 'DatabaseName', HelpMessage = 'The name of the SQL Server database.' )] [ValidateNotNullOrEmpty()] [string]$DatabaseName, ########################################################################### [Parameter( Position = 1, Mandatory = $false, ParameterSetName = 'DatabaseName', HelpMessage = 'The SQL Server instance name.' )] [string]$Server = '.', ########################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Transaction isolation level.' )] [ValidateSet('ReadCommitted', 'ReadUncommitted', 'RepeatableRead', 'Serializable', 'Snapshot', 'Chaos')] [string]$IsolationLevel = "ReadCommitted", ########################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Automatically consent to this installation type and set persistent flag..' )] [switch] $AutoConsent, ########################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Automatically consent to ALL third-party software installations and set persistent flag for SQL Server package.' )] [switch] $AutoConsentAllPackages, ########################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Use alternative settings stored in session for ' + 'preferences') )] [switch]$SessionOnly ########################################################################### ) begin { # load SQL Server client assembly with embedded consent using Copy-IdenticalParamValues $ensureParams = GenXdev\Copy-IdenticalParamValues ` -BoundParameters $PSBoundParameters ` -FunctionName 'GenXdev\EnsureNuGetAssembly' ` -DefaultValues ( Microsoft.PowerShell.Utility\Get-Variable -Scope Local ` -ErrorAction SilentlyContinue ) # Set specific parameters for SQL Server package $ensureParams['PackageKey'] = 'System.Data.SqlClient' $ensureParams['Description'] = 'SQL Server client library required for database operations' $ensureParams['Publisher'] = 'Microsoft' GenXdev\EnsureNuGetAssembly @ensureParams # initialize connection string from parameters if ($PSCmdlet.ParameterSetName -eq 'DatabaseName') { $connString = "Server=$Server;Database=$DatabaseName;Integrated Security=true;TrustServerCertificate=true" } else { $connString = $ConnectionString } Microsoft.PowerShell.Utility\Write-Verbose "Creating SQL Server transaction with connection string: $connString" } process { try { # establish database connection $connectionClass = "System.Data.SqlClient.SqlConnection" $connection = Microsoft.PowerShell.Utility\New-Object $connectionClass($connString) $connection.Open() Microsoft.PowerShell.Utility\Write-Verbose 'SQL Server connection opened successfully' # begin transaction with specified isolation $transaction = $connection.BeginTransaction($IsolationLevel) Microsoft.PowerShell.Utility\Write-Verbose "Transaction started with $IsolationLevel isolation level" # return the transaction object return $transaction } catch { if ($null -ne $connection -and $connection.State -eq 'Open') { $connection.Close() } throw $_ } } end { } } |