看我的几个相关函数(要耐心)
// check server ÊÇ·ñ´æÔÚ
function checkserver(servername:string):boolean;
var
app:_application;
nl:namelist ;
i:integer;
begin
result:=false;
app:=coapplication.Create;
nl:=app.ListAvailableSQLServers;
for i:=0 to nl.Count-1 do
begin
if uppercase(nl.Item(i+1))=uppercase(servername) then
begin
result:=true;
break;
end;
end;
end;
// check database
function checkdatabase(servername,databasename,username,password:string):boolean;
var
server:_sqlserver;
i:integer;
begin
result:=false;
if not checkserver(servername) then
begin
application.MessageBox(pchar('Êý¾Ý¿â·þÎñÆ÷'+servername+'²»´æÔÚ'), pchar('ÌáʾÐÅÏ¢'), MB_OK or MB_ICONINFORMATION);
exit;
end;
server:=cosqlserver.Create;
server.LoginTimeout:=-1;
With Server do
begin
LoginSecure := False;
AutoReConnect := False;
try
Connect(servername,username,password);
except
forms.application.MessageBox(pchar('Óû§Ãû»ò¿ÚÁîÓÐÎó'), pchar('ÌáʾÐÅÏ¢'), MB_OK or MB_ICONINFORMATION);
exit;
end;
end;
for i:=1 to server.Databases.Count do
begin
if uppercase(server.Databases.Item(i,'').Name)=uppercase(databasename) then
begin
result:=true;
break;
end;
end;
if not result then application.MessageBox(pchar('Êý¾Ý¿â '+databasename+'²»´æÔÚ'), pchar('ÌáʾÐÅÏ¢'), MB_OK or MB_ICONINFORMATION);
end;
//sql server backup database
function backupdatabase(servername,databasename,username,password,filename:string):boolean;
var
server:_sqlserver;
Backup :_Backup;
begin
result:=false;
if not checkdatabase(servername,databasename,username,password) then
begin
exit;
end;
server:=cosqlserver.Create;
server.LoginTimeout:=-1;
With Server do
begin
LoginSecure := False;
AutoReConnect := False;
Connect(servername,username,password);
end;
Backup :=coBackup.Create ;
backup.Database:=databasename;
Backup.Files := filename;
Screen.Cursor:= crHourGlass;
try
backup.SQLBackup(server);
Screen.Cursor := crDefault;
forms.application.MessageBox(pchar('Êý¾Ý¿â '+databasename+' ±¸·Ý³É¹¦'), pchar('Êý¾Ý¿â±¸·Ý'), MB_OK or MB_ICONINFORMATION);
result:=true;
except
Screen.Cursor := crDefault;
forms.application.MessageBox(pchar('Êý¾Ý¿â '+DatabaseName+' ±¸·Ýʧ°Ü'), pchar('Êý¾Ý¿â±¸·Ý'), MB_OK or MB_ICONINFORMATION);
end;
end;
//sql server restore database
function restoredatabase(servername,databasename,username,password,filename:string):boolean;
var
server:_sqlserver;
Restore :_Restore;
begin
result:=false;
server:=cosqlserver.Create;
server.LoginTimeout:=-1;
With Server do
begin
LoginSecure := False;
AutoReConnect := False;
Connect(servername,username,password);
end;
Restore :=coRestore.create;
Restore.Database :=DatabaseName;
Restore.Replacedatabase:=true;
Restore.action:=SQLDMORESTORE_DATABASE;
Restore.Files := filename;
try
Screen.Cursor := crHourGlass;
Restore.SQLRestore(server);
Screen.Cursor := crDefault;
application.MessageBox(pchar('Êý¾Ý¿â '+DatabaseName+' »Ö¸´³É¹¦'), pchar('Êý¾Ý¿â»Ö¸´'), MB_OK or MB_ICONINFORMATION);
except
Screen.Cursor := crDefault;
application.MessageBox(pchar('Êý¾Ý¿â '+DatabaseName+' »Ö¸´Ê§°Ü'), pchar('Êý¾Ý¿â»Ö¸´'), MB_OK or MB_ICONINFORMATION);
end;
end;
//get servername from connectionstring
function getservername(connectionstring:string):string;
var
tmp,s:string;
begin
s:=connectionstring;
while pos(';',s)>0 do
begin
tmp:=copy(s,1,pos(';',s));
s:=copy(s,pos(';',s)+1,length(s)-pos(';',s));
if pos(uppercase('Data Source='),uppercase(tmp))>0 then
begin
result:=copy(tmp,pos('=',tmp)+1,length(tmp)-pos('=',tmp)-1);
exit;
end;
end;
tmp:=s;
if pos(uppercase('Data Source='),uppercase(tmp))>0 then
begin
result:=copy(tmp,pos('=',tmp)+1,length(tmp)-pos('=',tmp)-1);
end;
end;
//get databasename from connectionstring
function getdatabasename(connectionstring:string):string;
var
tmp,s:string;
begin
s:=connectionstring;
while pos(';',s)>0 do
begin
tmp:=copy(s,1,pos(';',s));
s:=copy(s,pos(';',s)+1,length(s)-pos(';',s));
if pos(uppercase('Initial Catalog='),uppercase(tmp))>0 then
begin
result:=copy(tmp,pos('=',tmp)+1,length(tmp)-pos('=',tmp)-1);
exit;
end;
end;
tmp:=s;
if pos(uppercase('Initial Catalog='),uppercase(tmp))>0 then
begin
result:=copy(tmp,pos('=',tmp)+1,length(tmp)-pos('=',tmp)-1);
end;
end;
//get username from connectionstring
function getusername(connectionstring:string):string;
var
tmp,s:string;
begin
s:=connectionstring;
while pos(';',s)>0 do
begin
tmp:=copy(s,1,pos(';',s));
s:=copy(s,pos(';',s)+1,length(s)-pos(';',s));
if pos(uppercase('User ID='),uppercase(tmp))>0 then
begin
result:=copy(tmp,pos('=',tmp)+1,length(tmp)-pos('=',tmp)-1);
exit;
end;
end;
tmp:=s;
if pos(uppercase('User ID='),uppercase(tmp))>0 then
begin
result:=copy(tmp,pos('=',tmp)+1,length(tmp)-pos('=',tmp)-1);
end;
end;
//get password from connectionstring
function getpassword(connectionstring:string):string;
var
tmp,s:string;
begin
s:=connectionstring;
while pos(';',s)>0 do
begin
tmp:=copy(s,1,pos(';',s));
s:=copy(s,pos(';',s)+1,length(s)-pos(';',s));
if pos(uppercase('Password='),uppercase(tmp))>0 then
begin
result:=copy(tmp,pos('=',tmp)+1,length(tmp)-pos('=',tmp)-1);
exit;
end;
end;
tmp:=s;
if pos(uppercase('Password='),uppercase(tmp))>0 then
begin
result:=copy(tmp,pos('=',tmp)+1,length(tmp)-pos('=',tmp)-1);
end;
end;