Ver Mensaje Individual
  #1  
Antiguo 16-11-2005
SSAtab SSAtab is offline
Registrado
 
Registrado: nov 2005
Posts: 1
Reputación: 0
SSAtab Va por buen camino
Hacer backup de BD de SQLServer desde Delphi

Saludos Gente, como buen forero aqui les traigo un tema/reto para los expertos .

Pues el tema es el siguiente, tengo una base de datos en MS SQLServer y necesito crear un programa en delphi con el cual pueda hacer un respaldo de esa BD, he investigado un poco y me he encontrado links en ingles sobre este tema pero no termino por entenderles bien. Usan librerias como SQLDMO, SQLNS, pero no termino de entender bien los pasos.

Aqui les pongo este codigo:

Cita:
If you have Enterprise manager (SQL Server tools) installed on the machine, you may activate any Enterprise Manager function or menu from within your appliction using SQLNS and SQLDMO.
The good thing is it is easy to make backup as well as restore since it works exactly the same way as using the Enterprise manager. The downside is it will only work if the tools are installed on the client machine.
1) Choose project, Import typelibrary.
2) Look for "Microsoft SQLNamespace Object Library" in the list.
3) Choose install or create unit depending on if you want it as a component or just access using TLB unit as I have.
4) Include SQLNS.pas in uses of your unit.
5) Now do the same 4 steps with "Microsoft SQLDMO Object library" (SQLDMO.pas)
First operation is then to connect to the SQL server...
uses
SQLNS, SQLDMO;
var
FNameSpace:SQLNamespace; // This one needs to be global or part of class as it needs to be accessed by other functions.
var
ConString:OLEVariant;
Hnd:Integer;
FServerName, FPass, FUser: String;
// strings are to be assigned with the password, username and servername to then make part of connection string...
ConString:='Server='+FServerName+';UID='+FUser+';PWD='+FPass+';Trusted_Connection=No';
// The connect operation also requires a window handle. As I had this call within a component I used the one below. If you have a form then Hnd:=Form.Handle instead.
Hnd:=(Owner as TWinControl).Handle;
// Now make the connection...
FNameSpace:= CoSQLNamespace.Create;
FNameSpace.Initialize(FAppName,SQLNSRootType_Server,ConString,Hnd);
// Include the function below to navigate easy in the namespace...
function GetDBNameSpaceObject(
const aDBName: String): SQLNamespaceObject;
var
hServer,hDatabases,hDatabase:Integer;
begin
Result:=nil;
hServer:=FNameSpace.GetRootItem;
hDatabases:=FNameSpace.GetFirstChildItem(hServer,SQLNSOBJECTTYPE_DATABASES,'');
hDatabase:=FNameSpace.GetFirstChildItem(hDatabases,SQLNSOBJECTTYPE_DATABASE,aDBName);
Result:=FNameSpace.GetSQLNameSpaceObject(hDatabase);
end;
// Now you may use this function to activate the "database backup menu" for a particular database on the server...
procedure ShowBackupDB(const aDBName: String);
var
DB:SQLNamespaceObject;
Hnd:Integer;
begin
Hnd:=(Owner as TWinControl).Handle;
DB:=GetDBNameSpaceObject(aDBName);
DB.ExecuteCommandByID(SQLNS_CmdID_DATABASE_BACKUP,Hnd,0);
DB:=nil;
end;
// To show "restore menu", use this procedure...
procedure ShowRestoreDB(const aDBName: String);
var
DB:SQLNamespaceObject;
Hnd:Integer;
begin
Hnd:=(Owner as TWinControl).Handle;
DB:=GetDBNameSpaceObject(aDBName);
DB.ExecuteCommandByID(SQLNS_CmdID_DATABASE_RESTORE,Hnd,0);
DB:=nil;
end;
// To make a backup without showing any interface, use this procedure...
{
RestoreType=SQLDMORestore_Database,
SQLDMORestore_Files,
SQLDMORestore_Log
DeviceName=Valid name of a backup-device
}
procedure RecordBackup(const aDBName: string;
BackupType: SQLDMO_BACKUP_TYPE; const DeviceName: string;
Initialize: Boolean);
var
ThisBackup:_Backup;
begin
try
ThisBackup:=coBackup.Create;
except
Messagedlg('coBackup.Create failed, the object might not be installed on this machine',mtError,[mbOK],0);
end;
ThisBackup.Initialize:=Initialize;
ThisBackup.Database:=aDBName;
ThisBackup.Devices:='['+DeviceName+']';
ThisBackup.SQLBackup(FServer);
ThisBackup:=nil;
end;
// To restore without interface, use this procedure...
procedure TNSConnection.RestoreBackup(const aDBName: string;
RestoreType: SQLDMO_RESTORE_TYPE; const DeviceName: string);
var
ThisRestore:_Restore;
begin
try
ThisRestore:=coRestore.Create;
except
Messagedlg('coRestore.Create failed, the object might not be installed on this machine',mtError,[mbOK],0);
end;
ThisRestore.Database:=aDBName;
ThisRestore.Devices:='['+DeviceName+']';
ThisRestore.Action:=RestoreType;
ThisRestore.SQLRestore(FServer);
end;
No se si haya otra forma de hacerlas a esta no le entendi muy bien.

Saludos gente!
Responder Con Cita