-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpen-SqlConnection.ps1
More file actions
42 lines (37 loc) · 1.13 KB
/
Open-SqlConnection.ps1
File metadata and controls
42 lines (37 loc) · 1.13 KB
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
<#
.SYNOPSIS
Open new SQL Connection.
.DESCRIPTION
Open a new SQL Connection object based on the $Stage flag for the Stage or Production database.
.PARAMETER Stage
Optional. If $false create a connection for the Production database. Default value is $true.
.OUTPUTS
System.Data.SqlClient.SqlConnection
.EXAMPLE
Open-SqlConnection -Stage $Stage
#>
function Open-SqlConnection() {
[cmdletbinding()]
Param(
[bool]$Stage = $true
)
Begin {
$connectionString = Get-ConnectionString -Stage $Stage
}
Process {
try {
$sqlConnection = new-object System.Data.SqlClient.SqlConnection;
$sqlConnection.ConnectionString = $connectionString;
$isCloseConnection = ($sqlConnection.State -eq [System.Data.ConnectionState]'Closed')
if ($isCloseConnection) {
$sqlConnection.Open()
}
return $sqlConnection;
}
catch {
Write-Exception -ErrorObj $_ -Message "Throws an exception in 'Open-SqlConnection'" -Stop $true
}
}
End {
}
}