唐兄,你好!感谢你对我的问题的关心。
我在网上已得到解决此问题的一些方法,请你试一下:
1.获得安装sqlserver服务器名称:
var
SQLServer:Variant;
ServerList:Variant;
i,nServers:integer;
sRetValue:String;
begin
SQLServer := CreateOleObject('SQLDMO.Application');
ServerList:= SQLServer.ListAvailableSQLServers;
nServers:=ServerList.Count;
for i := 1 to nservers do
ListBox1.Items.Add(ServerList.Item(i));
SQLServer:=NULL;
serverList:=NULL;
end;
2.启动sqlserver服务
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';
var
Reg: TRegistry;
begin
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;
end;
const
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;
var
S: string;
begin
Screen.Cursor := crHourGlass;
try
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;
finally
Screen.Cursor := crDefault;
end;
end;
function StopMSSQL: Boolean;
begin
Screen.Cursor := crHourGlass;
try
try
if not IsNT then
WinExec(MSSQL_98StopCommand, SW_HIDE) else
WinExec(MSSQL_NTStopCommand, SW_HIDE);
Result := True;
except
Result := False;
end;
finally
Screen.Cursor := crDefault;
end;
end;
以上两个答案我均已试过可行,再此感谢给我提供答案的网友,我已不记得名字了。