Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the result output from an SQL BACKUP command into a Delphi program?

The output of sql commands that is visible to users who interactively run SQL commands from SQL Server Management studio, is different than the output you get back from executing an ADO command or ADO query object.

USE [DBNAME] 
BACKUP DATABASE [DBNAME] TO 
 DISK = 'C:\SqlBackup\Backup.mdf'

The successful completion output is like this:

Processed 465200 pages for database 'DBNAME', file 'filename' on file 2.
Processed 2 pages for database 'DBNAME', file 'filename_log' on file 2.
BACKUP DATABASE successfully processed 465202 pages in 90.595 seconds (40.116 MB/sec).

When I execute either a TADOCommand or TADOQuery with the CommandText or SQL set as above, I do not get any such output. How do I read this "secondary output" from the execution of an SQL command? I'm hoping that perhaps via some raw ADO operations I might be able to execute a command and get back the information above, for success, as well as any errors in performing an Sql backup.

Update: The answer below works better for me than my naive attempt, which did not work, using plain Delphi TADOCommand and TADOConnection classes:

  • create TADOCommand and TADOConnection.
  • execute command.
  • get info-messages back.

The problem I experienced in my own coding attempts, is that my first command is "use dbname" and the only recordset I traversed in my code, was the results of the "use dbname" command, not the second command I was executing. The accepted answer below traverses all recordsets that come back from executing the ADO command, and thus it works much better. Since I'm doing all this in a background thread, I actually think it's better to create the raw Com Objects anyways, and avoid any VCL entanglement in my thread. The code below could be a nice component if anybody is interested, let me know and I might make an open source "SQL Backup for Delphi" component.

like image 353
Warren P Avatar asked Aug 10 '12 20:08

Warren P


People also ask

How do I backup a SQL Server database using a script?

Right-click the database that you wish to backup, point to Tasks, and then select Back Up.... In the Back Up Database dialog box, the database that you selected appears in the drop-down list (which you can change to any other database on the server).

What is SQL Server backup?

backup [noun]A copy of SQL Server data that can be used to restore and recover the data after a failure. A backup of SQL Server data is created at the level of a database or one or more of its files or filegroups.


1 Answers

Here is an example. I've tested it with D7 and MSSQL2000. And it adds to Memo1 all messages from server:

29 percent backed up.
58 percent backed up.
82 percent backed up.
98 percent backed up.
Processed 408 pages for database 'NorthWind', file 'Northwind' on file 1.
100 percent backed up.
Processed 1 pages for database 'NorthWind', file 'Northwind_log' on file 1.
BACKUP DATABASE successfully processed 409 pages in 0.124 seconds (26.962 MB/sec).

Also if it takes a long time consider to implement a WHILE loop not in the main thread.

uses AdoInt,ComObj;
.....

procedure TForm1.Button1Click(Sender: TObject);
var cmd  : _Command;
    Conn : _Connection;
    RA   : OleVariant;
    rs   :_RecordSet;
    n    : Integer;
begin
  Memo1.Clear;

  Conn := CreateComObject(CLASS_Connection) as _Connection;
  Conn.ConnectionString := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=NorthWind;Data Source=SQL_Server';
  Conn.Open(Conn.ConnectionString,'','',Integer(adConnectUnspecified));

  cmd := CreateComObject(CLASS_Command) as _Command;
  cmd.CommandType := adCmdText;
  cmd.Set_ActiveConnection(Conn);
  cmd.CommandText := 'BACKUP DATABASE [NorthWind] TO  DISK = N''c:\sql_backup\NorthWind'' WITH  INIT ,  NOUNLOAD ,  NAME = N''NortWind backup'',  NOSKIP ,  STATS = 10,  NOFORMAT;';
  rs:=cmd.Execute(RA,0,Integer(adCmdText));

  while (rs<>nil) do
  begin
   for n:=0 to(Conn.Errors.Count-1)do begin
    Memo1.Lines.Add(Conn.Errors.Item[n].Description);
   end;
   rs:=rs.NextRecordset(RA);
  end;

  cmd.Set_ActiveConnection(nil);
  Conn.Close;
  cmd  := nil;
  Conn := nil;
end;

I've found this thread (Russian) for stored procedure and correct it for BACKUP command.

like image 105
valex Avatar answered Oct 18 '22 08:10

valex