開發/測試機為加快開機速度,將資料庫服務設為「手動」,使用時需逐一啟動多項服務。批次腳本可一次完成,並支援雙向操作。
@echo off
chcp 65001 >nul
title SQL Server / Oracle 服務管理工具
:MENU
cls
echo ==========================================
echo SQL Server / Oracle 服務管理選單
echo ==========================================
echo [1] 啟動 SQL Server 所有服務
echo [2] 停止 SQL Server 所有服務
echo [3] 啟動 Oracle 服務
echo [4] 停止 Oracle 服務
echo [0] 結束
echo ==========================================
set /p choice=請輸入選項 (0-4):
if "%choice%"=="" goto MENU
if "%choice%"=="1" goto START_SQL
if "%choice%"=="2" goto STOP_SQL
if "%choice%"=="3" goto START_ORA
if "%choice%"=="4" goto STOP_ORA
if "%choice%"=="0" goto EXIT
echo 無效選項,請重新輸入.
timeout /t 2 >nul
goto MENU
:START_SQL
echo 正在啟動 SQL Server 服務...
net start "SQL Server (MSSQLSERVER)" 2>nul
net start "SQL Server Agent (MSSQLSERVER)" 2>nul
net start "SQL Server Browser" 2>nul
net start "SQL Server VSS Writer" 2>nul
net start "SQL Full-text Filter Daemon Launcher (MSSQLSERVER)" 2>nul
net start "ReportServer" 2>nul
net start "MsDtsServer150" 2>nul // 版本號視安裝而定
echo SQL Server 服務啟動指令已送出.
timeout /t 3 >nul
goto MENU
:STOP_SQL
echo 正在停止 SQL Server 服務...
net stop "SQL Server Agent (MSSQLSERVER)" 2>nul
net stop "ReportServer" 2>nul
net stop "MsDtsServer150" 2>nul
net stop "SQL Full-text Filter Daemon Launcher (MSSQLSERVER)" 2>nul
net stop "SQL Server VSS Writer" 2>nul
net stop "SQL Server Browser" 2>nul
net stop "SQL Server (MSSQLSERVER)" 2>nul
echo SQL Server 服務停止指令已送出.
timeout /t 3 >nul
goto MENU
:START_ORA
echo 正在啟動 Oracle 服務...
net start "OracleServiceORCL" 2>nul
net start "OracleOraDB19Home1TNSListener" 2>nul
net start "OracleDBConsoleORCL" 2>nul
echo Oracle 服務啟動指令已送出.
timeout /t 3 >nul
goto MENU
:STOP_ORA
echo 正在停止 Oracle 服務...
net stop "OracleDBConsoleORCL" 2>nul
net stop "OracleOraDB19Home1TNSListener" 2>nul
net stop "OracleServiceORCL" 2>nul
echo Oracle 服務停止指令已送出.
timeout /t 3 >nul
goto MENU
:EXIT
echo 作業結束,視窗將在 3 秒後關閉...
timeout /t 3 >nul
exit
db_services.bat。net start/stop 會拒絕存取。services.msc 確認確切「服務名稱」(非顯示名稱)。OracleOraDB19Home1),請依實際調整。$svcsSql = @('MSSQLSERVER','SQLSERVERAGENT','SQLBrowser','SQLWriter','MSSQLFDLauncher','ReportServer','MsDtsServer150')
$svcsOra = @('OracleServiceORCL','OracleOraDB19Home1TNSListener','OracleDBConsoleORCL')
function Toggle-Services ($names, $action) {
foreach ($n in $names) {
$svc = Get-Service $n -ErrorAction SilentlyContinue
if (-not $svc) { Write-Warning "服務 $n 不存在"; continue }
try {
if ($action -eq 'Start') { Start-Service $svc -ErrorAction Stop }
else { Stop-Service $svc -ErrorAction Stop }
Write-Host "[$action] $($svc.DisplayName) - 成功" -Fore Green
} catch { Write-Error "[$action] $($svc.DisplayName) - 失敗: $_" }
}
}
do {
$choice = Read-Host "1=啟動SQL 2=停止SQL 3=啟動Oracle 4=停止Oracle 0=離開"
switch ($choice) {
1 { Toggle-Services $svcsSql 'Start' }
2 { Toggle-Services $svcsSql 'Stop' }
3 { Toggle-Services $svcsOra 'Start' }
4 { Toggle-Services $svcsOra 'Stop' }
0 { break }
default { Write-Warning "無效選項" }
}
} while ($true)
PowerShell 版更健壯、支援遠端執行(Invoke-Command),建議生產環境採用。