Jun 11, 2026, 12:22 PM
This commit is contained in:
+33
-3
@@ -49,6 +49,9 @@ $btnAddServer = $Window.FindName("btnAddServer")
|
||||
$btnRemoveServer = $Window.FindName("btnRemoveServer")
|
||||
$gridDetails = $Window.FindName("gridDetails")
|
||||
|
||||
# Initialize connection states cache per server
|
||||
$script:ServerConnectionStates = @{}
|
||||
|
||||
# Set initial values from settings
|
||||
$txtServerName.Text = $settings.LastServer
|
||||
|
||||
@@ -141,17 +144,38 @@ $tvServers.Add_SelectedItemChanged({
|
||||
$gridDetails.Visibility = "Visible"
|
||||
|
||||
if ($selectedItem.Tag -is [string]) {
|
||||
# It's a server node, copy its name to TextBox
|
||||
$txtServerName.Text = $selectedItem.Tag
|
||||
Write-Log "Selected server: $($selectedItem.Tag)"
|
||||
# It's a server node, copy its name to TextBox and update its cached connection state
|
||||
$srvName = $selectedItem.Tag
|
||||
$txtServerName.Text = $srvName
|
||||
Write-Log "Selected server: $srvName"
|
||||
|
||||
# Check cached connection state for this server
|
||||
$connState = $script:ServerConnectionStates[$srvName]
|
||||
if ($null -ne $connState -and $connState.Success) {
|
||||
# Server is connected: show its version/details in the DataGrid
|
||||
$dgServerInfo.ItemsSource = @([PSCustomObject]$connState | Select-Object Instance, Version, Edition, Status)
|
||||
} else {
|
||||
# Server is not connected: clear the DataGrid so no old server details are visible
|
||||
$dgServerInfo.ItemsSource = $null
|
||||
}
|
||||
} elseif ($selectedItem.Tag -is [System.Management.Automation.PSCustomObject]) {
|
||||
# It's a database node
|
||||
$db = $selectedItem.Tag
|
||||
Write-Log "Selected database: $($db.Name) (Size: $($db.Size), Status: $($db.Status))"
|
||||
|
||||
# Show database details in the main information grid
|
||||
$dgServerInfo.ItemsSource = @($db | Select-Object Name, Status, Size, RecoveryModel)
|
||||
|
||||
# Also, update txtServerName with the parent server's tag so they see which server it belongs to
|
||||
$parentItem = $selectedItem.Parent
|
||||
if ($null -ne $parentItem -and $parentItem.Tag -is [string]) {
|
||||
$txtServerName.Text = $parentItem.Tag
|
||||
}
|
||||
}
|
||||
} else {
|
||||
# Hide detail panel if selection is null or ROOT
|
||||
$gridDetails.Visibility = "Collapsed"
|
||||
$dgServerInfo.ItemsSource = $null
|
||||
}
|
||||
})
|
||||
|
||||
@@ -231,11 +255,17 @@ $btnConnect.Add_Click({
|
||||
if ($result.Success) {
|
||||
Write-Log "Connected successfully to $($result.Instance)!"
|
||||
$dgServerInfo.ItemsSource = @([PSCustomObject]$result | Select-Object Instance, Version, Edition, Status)
|
||||
# Cache successful connection state
|
||||
$script:ServerConnectionStates[$server] = $result
|
||||
} else {
|
||||
Write-Log "Connection failed: $($result.Error)"
|
||||
$script:ServerConnectionStates[$server] = $null
|
||||
$dgServerInfo.ItemsSource = $null
|
||||
}
|
||||
} catch {
|
||||
Write-Log "Critical Error: $($_.Exception.Message)"
|
||||
$script:ServerConnectionStates[$server] = $null
|
||||
$dgServerInfo.ItemsSource = $null
|
||||
} finally {
|
||||
$btnConnect.IsEnabled = $true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user