@@ -10,14 +10,19 @@ using namespace System.Diagnostics.CodeAnalysis
1010 The newly created database connection.
1111#>
1212function New-SqlConnection {
13- [CmdletBinding ()]
13+ [CmdletBinding (DefaultParameterSetName = " Type " )]
1414 [OutputType ([System.Data.IDbConnection ])]
1515 [SuppressMessage (" PSUseShouldProcessForStateChangingFunctions" , " " )]
1616 param (
1717 # The type of connection class to instantiate.
18- [Parameter (Mandatory , Position = 0 )]
18+ [Parameter (Mandatory , ParameterSetName = " Type " , Position = 0 )]
1919 [Type ] $Type ,
2020
21+ # The name of an ADO.NET provider.
22+ [Parameter (Mandatory , ParameterSetName = " Provider" , Position = 0 )]
23+ [ValidateSet (" Odbc" , " OleDb" , " SqlClient" )]
24+ [string ] $Provider ,
25+
2126 # The connection string used to open the database.
2227 [Parameter (Mandatory , Position = 1 , ValueFromPipeline )]
2328 [string ] $ConnectionString ,
@@ -27,7 +32,14 @@ function New-SqlConnection {
2732 )
2833
2934 process {
30- $connection = [IDbConnection ] [Activator ]::CreateInstance($Type , $ConnectionString )
35+ $class = switch ($Provider ) {
36+ " Odbc" { [Odbc.OdbcConnection ]; break }
37+ " OleDb" { [OleDb.OleDbConnection ]; break }
38+ " SqlClient" { [SqlClient.SqlConnection ]; break }
39+ default { $Type }
40+ }
41+
42+ $connection = [IDbConnection ] [Activator ]::CreateInstance($class , $ConnectionString )
3143 if ($Open ) { $connection.Open () }
3244 $connection
3345 }
0 commit comments