Set-DbConnectionString.ps1

$fmgConnectionString = $null;
$fmgParameterPrefix = "@";
[System.Data.Common.DbProviderFactory] $fmgDbFactory = $null;


function Get-DbConnectionString() {
<#
    .SYNOPSIS
    Gets the default global connection string
 
    .DESCRIPTION
    This function is called in absense of a specified connection string
    for functions that require them.
 
    .EXAMPLE
    $connectionString = Get-DbConnectionString
#>

    $fmgConnectionString = Get-Variable -Name "fmgConnectionString" -Scope Script -ErrorAction SilentlyContinue
    if($fmgConnectionString) {
        return $fmgConnectionString.Value;
    }
    return $Null;
}

function Set-DbConnectionString() {
<#
    .SYNOPSIS
    Sets the default global connection string and optionally the
    provider used to create the DbProviderFactory.
 
    .DESCRIPTION
    An alternate ConvertTo-Json method that outputs readable json unlike
    the native version for Powershell 5 and below.
 
    .PARAMETER ConnectionString
    The string of key pair values that is used to construct a connection
    to a resource such as a database server.
 
    .PARAMETER ProviderName
    (Optional) The of the Database Provider Factory such as
    "System.Data.SqlClient", "MySql.Data.MySqlClient", "Npgsql2 Data Provider"
 
    .EXAMPLE
    Set-DbConnectionString "Data Source=(LocalDB)\MSSQLLocalDB;Integrated Security=True"
 
#>

    Param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string] $ConnectionString,
        [string] $ProviderName = "System.Data.SqlClient"
    )

    $fmgConnectionString = $ConnectionString;
    $factory = New-DbProviderFactory -ProviderName $ProviderName
    $factory | Set-DbProviderFactory
    Set-Variable -Name "fmgConnectionString" -Value $ConnectionString -Scope Script 
}

function New-DbProviderFactory() {
<#
    .SYNOPSIS
    Creates a new instance of DbProviderFactory by name.
 
    .DESCRIPTION
    Internally uses `System.Data.Common.DbProviderFactories` to create
    a new DbProviderFactory instance.
 
    .PARAMETER ProviderName
    The of the Database Provider Factory such as
    "System.Data.SqlClient", "MySql.Data.MySqlClient", "Npgsql2 Data Provider"
 
    .EXAMPLE
    $factory = New-DbProviderFactory "System.Data.SqlClient"
 
#>

    Param(
        [Parameter(Mandatory = $true, Position = 1)]
        [string] $ProviderName
    )


    return [System.Data.Common.DbProviderFactories]::GetFactory($ProviderName);
}

function Set-DbProviderFactory() {
<#
    .SYNOPSIS
    Sets the default global provider factory.
 
    .DESCRIPTION
    The default global provider factory is used by the other functions /
    cmdlets in the this module to construct Connection, Commands, Transaction
    and Parameter objects when a provider factory is not specified.
 
    .PARAMETER Factory
    An instance of `System.Data.Common.DbProviderFactory`
 
    .EXAMPLE
    Set-DbProviderFactory ([MySql.Data.MySqlClient.MySqlClientFactory]::Instance)
 
#>

    Param(
        [Parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true)]
        [System.Data.Common.DbProviderFactory] $Factory
    )

    Set-Variable -Name "fmgDbFactory" -Value $Factory -Scope Script
}

function Get-DbProviderFactory() {
<#
    .SYNOPSIS
    Gets the default SqlProviderFactory or the one specified by the
    ProviderName parameter.
 
    .DESCRIPTION
    The default global provider factory is used by the other functions /
    cmdlets in the this module to construct Connection, Commands, Transaction
    and Parameter objects when a provider factory is not specified.
 
    .PARAMETER ProviderName
    An instance of `System.Data.Common.DbProviderFactory`
 
    .EXAMPLE
    $factory = Get-DbProviderFactory
 
    .EXAMPLE
    $factory = Get-DbProviderFactory "System.Data.SqlClient"
 
#>

    Param(
        [Parameter(Position = 0)]
        [string] $ProviderName
    )

    if($gnomeDbFactory) {
        return $gnomeDbFactory;
    }

    if([string]::IsNullOrWhiteSpace($ProviderName)) {
        $ProviderName = "System.Data.SqlClient";
    }

    $factory = New-DbProviderFactory $ProviderName
    $factory | Set-DbProviderFactory

    return $factory
}


function Get-DbParameterPrefix() {
<#
    .SYNOPSIS
    Gets the default sql parameter prefix such as '@'
 
    .DESCRIPTION
    This function is called in absense of a specified parameter prefix
    for many of the functions / cmdlets in this modules.
 
    .EXAMPLE
    $parameterPrefix = Get-DbParameterPrefix
#>

    $fmgParameterPrefix = Get-Variable -Name "fmgParameterPrefix" -Scope Script -ErrorAction SilentlyContinue
    if($fmgParameterPrefix) {
        return $fmgParameterPrefix.Value;
    }

    return $Null;
}