views:

317

answers:

2

I know this is most-likely a simple question but when you restore a database from inside SQL management studio you can set the update interval with stats

RESTORE DATABASE [test] FROM  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\test.bak' WITH  FILE = 1,  NOUNLOAD,  STATS = 10

If I wanted to execute that line of code from inside c# how would i get the progress? Currently I just use System.Data.SqlClient.SqlCommand.ExecuteNonQuery() but I can not figure out how to get the progress.

Also, if it is any faster, using the Microsoft.SQLServer namespace is acceptable.

+2  A: 

ExecuteNonQuery is only going to return once the operation is complete. There might be a way to monitor its progress from a connection on another thread or to use an async call, but you could also look at using SMO, which provides a way to register callbacks see http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.restore.aspx and http://msdn.microsoft.com/en-us/library/ms162133.aspx with PercentComplete event

Cade Roux
Do you know if the SQL2008 SDK is backwards compatible with the 2005 and 2000 servers OR is the SQL2000 SDK forward compatible. (I need to administer a 2000 and 2005 instance on the same server and some point in the future a 2008 instance)
Scott Chamberlain
@Scott Chamberlain I would use the latest SDK and test against the old servers. That has the highest likelihood of success. The 2000 SDK didn't even have SMO (it had something called SQL-DMO).
Cade Roux
@Scott Chamberlain In SQL Server 2000, for Remus' solution, you will need to use the sysprocesses table, because that DMV was not available in that version.
Cade Roux
In researching SMO's they work with compatibility level 80 (sql2000) and up.
Scott Chamberlain
+3  A: 

Before you start the operation get the connection session id:

SELECT @@SPID;

Then start your backup request. From a different connection, query sys.dm_exec_requests and look at percent_complete for the session that executes the restore statement:

Percentage of work completed for the following commands:

  • ALTER INDEX REORGANIZE
  • AUTO_SHRINK option with ALTER DATABASE
  • BACKUP DATABASE
  • CREATE INDEX
  • DBCC CHECKDB
  • DBCC CHECKFILEGROUP
  • DBCC CHECKTABLE
  • DBCC INDEXDEFRAG
  • DBCC SHRINKDATABASE
  • DBCC SHRINKFILE
  • KILL (Transact-SQL)
  • RESTORE DATABASE
  • UPDATE STATISTICS.
Remus Rusanu