delphi下檢查sql server服務(wù)器當(dāng)前運(yùn)行狀態(tài)!
要求:delphi下隨時監(jiān)控sql?。螅澹颍觯澹驙顟B(tài)。
1、命令提示符下可用:telnet <sql server ip> 1433檢查。
2、delphi下如何模擬實(shí)現(xiàn)上述功能。
———-
原來做過:
function getsqlserverstatus(lpszcomputername: lpctstr): integer;
var
ssstatus: service_status;
dwoldcheckpoint: dword;
dwstarttickcount: dword;
dwwaittime: dword;
dwstatus: dword;
lpszservicename: lpctstr;
schscmanager: sc_handle;
schservice: sc_handle;
begin
if (lpszcomputername <> nil) and
((strcomp(lpszcomputername, \\\’127.0.0.1\\\’) = 0) or (strcomp(lpszcomputername, \\\’.\\\’) = 0)) then
lpszcomputername := nil;
lpszservicename := \\\’mssqlserver\\\’;
schscmanager := openscmanager(
lpszcomputername, //computer name
nil, // servicesactive database
sc_manager_all_access); // full access rights
if schscmanager = 0 then
getsqlserverstatus := -1; //machine not exists
schservice := openservice(
schscmanager, // scm database
lpszservicename, // service name
service_all_access);
if schservice = 0 then begin
closeservicehandle(schservice);
getsqlserverstatus := -2; //sqlserver service not exists
end;
if not queryservicestatus(
schservice, // handle to service
ssstatus) then begin // address of status information structure
getsqlserverstatus := -3; //myerrorexit(\\\’queryservicestatus\\\’);
result := ssstatus.dwcurrentstate;
end;
//———-
網(wǎng)上找的,看看有用不
uses registry, shellapi, filectrl, unit2;
{$r *.dfm}
function isnt: boolean;
begin
result := (win32majorversion >= 4) and (win32platform = ver_platform_win32_nt);
end;
var
isexists: boolean = false;
function isexistsmssql: boolean;
const
mssqlserver = \\\’software/microsoft/mssqlserver\\\’;
reg: tregistry;
result := isexists;
if result then exit;
if not isnt then
reg := tregistry.create else
reg := tregistry.create(key_read);
with reg do
try
reg.rootkey := hkey_local_machine;
result := keyexists(mssqlserver);
isexists := result;
finally
free;
end;
mssql_98startcommand = \\\’scm -action 1 -pwd "%s"\\\’;
mssql_ntstartcommand = \\\’net start mssqlserver\\\’;
mssql_98stopcommand = \\\’scm -action 6\\\’;
mssql_ntstopcommand = \\\’net stop mssqlserver\\\’;
function startmssql(pass: string): boolean;
s: string;
screen.cursor := crhourglass;
if not isnt then
s := format(mssql_98startcommand, [pass]) else
s := mssql_ntstartcommand;
try
winexec(pchar(s), sw_hide);
result := true;
except
result := false;
end;
screen.cursor := crdefault;
function stopmssql: boolean;
winexec(mssql_98stopcommand, sw_hide) else
winexec(mssql_ntstopcommand, sw_hide);
procedure tform1.button2click(sender: tobject);
if startmssql(edpass.text) then
messagebox(handle, \\\’啟動完成\\\’, \\\’完成\\\’, mb_ok mb_iconinformation);
———————————————-
program project1;
uses
windows,
winsvc;
procedure runmssqlservice;
srvhandle: sc_handle;
service_status: _service_status;
srvstatus : integer;
try
srvhandle := openscmanager(\\\’\\\’, services_active_database, sc_manager_all_access);
srvhandle := openservice(srvhandle, pchar(\\\’mssqlserver\\\’), service_query_status or service_start);
if queryservicestatus(srvhandle, service_status)
then
srvstatus := service_status.dwcurrentstate;
if srvstatus = service_stopped
winexec(\\\’scm -action 1 -slient 1 -service mssqlserver \\\’,sw_normal);
except
runmssqlservice;
end.
獲取sql server服務(wù)器列表的幾種方法
一、 sql dmo
描述:sql distributed management objects(sql分布式管理對象),存在于sqldmo.dll文件中,實(shí)際上是一個com 對象,通過調(diào)用sql dmo的listavailablesqlservers方法取得。