Skip to content

Commit

Permalink
Connect-DbaInstance, honor -Database parameter (#9585)
Browse files Browse the repository at this point in the history
  • Loading branch information
niphlod authored Feb 24, 2025
1 parent a074e4e commit 6b53882
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions public/Connect-DbaInstance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,22 @@ function Connect-DbaInstance {
$sqlConnectionInfo.Password = $csb.Password
$null = $csb.Remove('Password')
}
# look for 'Initial Catalog' and 'Database' in the connection string
$specifiedDatabase = $csb['Database']
if ($specifiedDatabase -eq '') {
$specifiedDatabase = $csb['Initial Catalog']
}
if ($Database -and $Database -ne $specifiedDatabase) {
Write-Message -Level Debug -Message "Database specified in connection string '$specifiedDatabase' does not match Database parameter '$Database'. Database parameter will be used."
# clear both, in order to not be overridden later by setting all AddtionalParameters
if ($csb.ShouldSerialize('Database')) {
$csb.Remove('Database')
}
if ($csb.ShouldSerialize('Initial Catalog')) {
$csb.Remove('Initial Catalog')
}
$sqlConnectionInfo.DatabaseName = $Database
}

# Add all remaining parts of the connection string as additional parameters.
$sqlConnectionInfo.AdditionalParameters = $csb.ConnectionString
Expand Down

0 comments on commit 6b53882

Please sign in to comment.