forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
New-DbProviderObject.ps1
108 lines (97 loc) · 3.39 KB
/
New-DbProviderObject.ps1
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<#
.SYNOPSIS
Create a common database object.
.INPUTS
System.String to initialize the database object.
.OUTPUTS
System.Data.Common.DbCommand (e.g. System.Data.SqlClient.SqlCommand) or
System.Data.Common.DbConnection (e.g. System.Data.SqlClient.SqlConnection) or
System.Data.Common.DbConnectionStringBuilder (e.g. System.Data.SqlClient.SqlConnectionStringBuilder),
as requested.
.FUNCTIONALITY
Database
.LINK
https://msdn.microsoft.com/library/system.data.common.dbproviderfactories.aspx
.EXAMPLE
New-DbProviderObject.ps1 ConnectionStringBuilder 'Server=(localdb)\ProjectsV13;Database=AdventureWorks;Integrated Security=SSPI;Encrypt=True'
Key Value
--- -----
Data Source (localdb)\ProjectsV13
Initial Catalog AdventureWorks
Integrated Security True
Encrypt True
.EXAMPLE
$conn = New-DbProviderObject.ps1 Connection $connstr -Open
($conn contains an open DbConnection object.)
.EXAMPLE
$cmd = New-DbProviderObject.ps1 Command -ConnectionString $connstr -Provider Odbc -StoredProcedure -OpenConnection
($cmd contains an OdbcCommand with a CommandType of StoredProcedure and an open connection to $connstr.)
#>
#Requires -Version 7
[CmdletBinding()][OutputType([Data.Common.DbCommand])]
[OutputType([Data.Common.DbConnection])][OutputType([Data.Common.DbConnectionStringBuilder])] Param(
# The type of object to create.
[ValidateSet('Command','Connection','ConnectionStringBuilder')]
[Parameter(Mandatory=$true,Position=0)][string] $TypeName,
<#
A value to initialize the object with, such as CommandText for a Command object, or
a ConnectionString for a Connection or ConnectionStringBuilder.
#>
[Parameter(Position=2,ValueFromPipeline=$true)][Alias('Value')][string] $InitialValue,
# The DbProviderFactory subclass to use to create the object.
[ValidateSet('Odbc','OleDb','Oracle','Sql')][string] $Provider = 'Sql',
<#
A connection string to use (when creating a Command object).
No connection will be made if not specified.
#>
[Parameter(Position=3)][Alias('CS')][string] $ConnectionString,
<#
Sets the CommandType property of a Command object to StoredProcedure.
Ignored for other objects.
#>
[switch] $StoredProcedure,
# Opens the Connection object (or Command connection) if an InitialValue was provided, ignored otherwise.
[switch] $OpenConnection
)
Process
{
$factory = switch($Provider)
{
Odbc {[Data.Odbc.OdbcFactory]::Instance}
OleDb {[Data.OleDb.OleDbFactory]::Instance}
Oracle {[Data.OracleClient.OracleClientFactory]::Instance}
Sql {[Data.SqlClient.SqlClientFactory]::Instance}
}
$value = switch($TypeName)
{
Command {$factory.CreateCommand()}
Connection {$factory.CreateConnection()}
ConnectionStringBuilder {$factory.CreateConnectionStringBuilder()}
}
if($InitialValue)
{
switch($TypeName)
{
Command
{
$value.CommandText = $InitialValue
}
Connection
{
$value.ConnectionString = $InitialValue
if($OpenConnection) {$value.Open()}
}
ConnectionStringBuilder
{ # PowerShell must use the method form
$value.set_ConnectionString($InitialValue)
}
}
}
if($TypeName -eq 'Command')
{
if($StoredProcedure) {$obj.CommandType = 'StoredProcedure'}
if($ConnectionString)
{$obj.Connection = New-DbProviderObject.ps1 Connection $ConnectionString -Provider:$Provider -OpenConnection:$OpenConnection}
}
return $value
}