透過 OLEObject 建立 SQLDMO.SQLServer 物件,檢查連線狀態與服務狀態,依狀態執行對應動作(啟動、繼續、停止)。
CONSTANT INTEGER SQLDMOSvc_Unknown = 0 // 未知
CONSTANT INTEGER SQLDMOSvc_Running = 1 // 已啟動
CONSTANT INTEGER SQLDMOSvc_Paused = 2 // 暫停
CONSTANT INTEGER SQLDMOSvc_Stopped = 3 // 停止
CONSTANT INTEGER SQLDMOSvc_Starting = 4 // 啟動中(冷啟動)
CONSTANT INTEGER SQLDMOSvc_Stopping = 5 // 關閉中
CONSTANT INTEGER SQLDMOSvc_Continuing = 6 // 啟動中(暫停後續)
CONSTANT INTEGER SQLDMOSvc_Pausing = 7 // 暫停中
// 判斷 SQL Server 是否已啟動
st_status.text = '判斷 SQL Server 是否已經啟動 ...'
OLEObject PBObject
LONG ll_status
PBObject = CREATE OLEObject
ll_status = PBObject.ConnectToNewObject("SQLDMO.SQLServer")
STRING ls_serverName, ls_UserName, ls_PassWord
INTEGER li_Return
ls_ServerName = '218.71.42.229'
ls_UserName = 'sa'
ls_PassWord = ''
IF ll_status = 0 THEN // 連接成功
PBObject.Name = ls_ServerName
PBObject.LoginTimeout = 10
PBObject.LoginSecure = True // NT 驗證模式
// PBObject.LoginSecure = False // SQL Server 驗證模式
IF IsNull(PBObject) THEN Return
TRY
li_Return = PBObject.Status
CATCH (RunTimeError rte)
MessageBox("提示", "沒有安裝 SQL Server", StopSign!)
END TRY
CHOOSE CASE li_Return
CASE 1 // 已啟動
st_Status.text = ls_ServerName + ' 已經啟動 SQL Server 服務'
// PBObject.Stop() // 可選:停止服務
CASE 2 // 暫停
st_status.text = '啟動 ' + ls_ServerName + ' 上的 SQL Server ...(暫停續傳)'
PBObject.Continue()
CASE 3 // 停止
st_status.text = '啟動 ' + ls_ServerName + ' 上的 SQL Server ...(冷啟動)'
PBObject.Start(False, ls_ServerName, ls_UserName, ls_PassWord)
CASE 4 // 啟動中(冷啟動)
st_status.text = ls_ServerName + ' 上的 SQL Server 正在啟動(冷啟動)'
PBObject.Start(False, 'sa', '')
CASE 5 // 關閉中
st_status.text = ls_ServerName + ' 上的 SQL Server 正在關閉'
CASE 6 // 啟動中(暫停續傳)
st_status.text = ls_ServerName + ' 上的 SQL Server 正在啟動(暫停續傳)'
CASE 7 // 暫停中
st_status.text = ls_ServerName + ' 上的 SQL Server 正在暫停'
CASE 0 // 未知
st_status.text = '無法判斷 SQL Server 狀態'
END CHOOSE
END IF