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")
|
$btnRemoveServer = $Window.FindName("btnRemoveServer")
|
||||||
$gridDetails = $Window.FindName("gridDetails")
|
$gridDetails = $Window.FindName("gridDetails")
|
||||||
|
|
||||||
|
# Initialize connection states cache per server
|
||||||
|
$script:ServerConnectionStates = @{}
|
||||||
|
|
||||||
# Set initial values from settings
|
# Set initial values from settings
|
||||||
$txtServerName.Text = $settings.LastServer
|
$txtServerName.Text = $settings.LastServer
|
||||||
|
|
||||||
@@ -141,17 +144,38 @@ $tvServers.Add_SelectedItemChanged({
|
|||||||
$gridDetails.Visibility = "Visible"
|
$gridDetails.Visibility = "Visible"
|
||||||
|
|
||||||
if ($selectedItem.Tag -is [string]) {
|
if ($selectedItem.Tag -is [string]) {
|
||||||
# It's a server node, copy its name to TextBox
|
# It's a server node, copy its name to TextBox and update its cached connection state
|
||||||
$txtServerName.Text = $selectedItem.Tag
|
$srvName = $selectedItem.Tag
|
||||||
Write-Log "Selected server: $($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]) {
|
} elseif ($selectedItem.Tag -is [System.Management.Automation.PSCustomObject]) {
|
||||||
# It's a database node
|
# It's a database node
|
||||||
$db = $selectedItem.Tag
|
$db = $selectedItem.Tag
|
||||||
Write-Log "Selected database: $($db.Name) (Size: $($db.Size), Status: $($db.Status))"
|
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 {
|
} else {
|
||||||
# Hide detail panel if selection is null or ROOT
|
# Hide detail panel if selection is null or ROOT
|
||||||
$gridDetails.Visibility = "Collapsed"
|
$gridDetails.Visibility = "Collapsed"
|
||||||
|
$dgServerInfo.ItemsSource = $null
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -231,11 +255,17 @@ $btnConnect.Add_Click({
|
|||||||
if ($result.Success) {
|
if ($result.Success) {
|
||||||
Write-Log "Connected successfully to $($result.Instance)!"
|
Write-Log "Connected successfully to $($result.Instance)!"
|
||||||
$dgServerInfo.ItemsSource = @([PSCustomObject]$result | Select-Object Instance, Version, Edition, Status)
|
$dgServerInfo.ItemsSource = @([PSCustomObject]$result | Select-Object Instance, Version, Edition, Status)
|
||||||
|
# Cache successful connection state
|
||||||
|
$script:ServerConnectionStates[$server] = $result
|
||||||
} else {
|
} else {
|
||||||
Write-Log "Connection failed: $($result.Error)"
|
Write-Log "Connection failed: $($result.Error)"
|
||||||
|
$script:ServerConnectionStates[$server] = $null
|
||||||
|
$dgServerInfo.ItemsSource = $null
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
Write-Log "Critical Error: $($_.Exception.Message)"
|
Write-Log "Critical Error: $($_.Exception.Message)"
|
||||||
|
$script:ServerConnectionStates[$server] = $null
|
||||||
|
$dgServerInfo.ItemsSource = $null
|
||||||
} finally {
|
} finally {
|
||||||
$btnConnect.IsEnabled = $true
|
$btnConnect.IsEnabled = $true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user