Search code examples
asp.netsqlsql-serverasp-classicdatabase-restore

Restore sql doesnt fire


Im trying restore database from backup dynamically with application code

simple sql command for restore

con.execute("RESTORE FILELISTONLY FROM DISK='c:\old.bak' " & vbcrlf &_
        "RESTORE DATABASE newdb " & vbcrlf &_
        "FROM DISK='c:\old.bak' " & vbcrlf &_
        "WITH MOVE 'newdb' TO 'c:\newdb.mdf', " & vbcrlf &_
        "MOVE 'newdb_log' TO 'c:\newdb_log.ldf'")

but it doesn't fire, i mean no any errors tried check it with sql profiler and look correctly

RESTORE FILELISTONLY FROM DISK='c:\old.bak' 
RESTORE DATABASE newdb 
FROM DISK='c:\old.bak' 
WITH MOVE 'newdb' TO 'c:\newdb.mdf', 
MOVE 'newdb_log' TO 'c:\newdb_log.ldf'

if i run sql from sql profiler its works

how can u explain this issue?


Solution

  • Try killing all the users in the database your restoring to before running the restore, a restore will fail if there are any active connections to the db. There is example code of how to do this all over.

    Also make sure your connection string used when you call the restore from your application doesn't connect to the db your restoring to, but connects to something like master or msdb.