Link to home
Start Free TrialLog in
Avatar of vwalla
vwalla

asked on

SMO Restore SQL 2005 DB - Errors / VB.net

I am using the following code to try and restore a DB from a backup file in SQL server 2005/ VS 2008 (VB).  If I use res.SqlRestoreAsync(srvSql), I get no erros, but the DB does not restore and the files are not in the data location.  If I use res.SqlRestore(srvSql), I get an error that the restore operation failed on ".\SQLEXPRESS".  All info I have found so far to do this shows exactly the code below, but no one can see to solve the issue.  Thanks!

BackgroundWorker1.ReportProgress(0, "Checking Database...")
        Dim srvConn As New ServerConnection(".\SQLEXPRESS")
        srvConn.LoginSecure = True
        srvSql = New Server(srvConn)
        If Not File.Exists("C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\DTDB.mdf") Then
            BackgroundWorker1.ReportProgress(0, "Creating Database files...")
            Dim res As Restore = New Restore
            Dim strPath As String = My.Application.Info.DirectoryPath & "\BAK\DTDB.bak"
            Dim strDest As String = "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\DTDB.bak"
            Dim strSQLPath As String = "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data"
            File.Copy(strPath, strDest, True)
            res.Action = RestoreActionType.Database
            Dim bdi As New BackupDeviceItem(strDest, DeviceType.File)
            res.Devices.Add(bdi)
            res.Database = "DTDB"
            res.SetPassword("PW")
            res.ReplaceDatabase = True
            res.NoRecovery = False
            res.RelocateFiles.Add(New RelocateFile("DTDB", strSQLPath & "\DTDB.mdf"))
            res.RelocateFiles.Add(New RelocateFile("DTDB_Log", strSQLPath & "\DTDB.ldf"))
            res.SqlRestoreAsync(srvSql)
        End If
        BackgroundWorker1.ReportProgress(0, "Check Complete!")

Open in new window

Avatar of 13598
13598
Flag of United States of America image

Try adding this line before res.ReplaceDatabase = True  :
res.LastRestore = True
Avatar of vwalla
vwalla

ASKER

LastRestore is not a member of...
nvm,I thought you were using  SQLDMO
Avatar of vwalla

ASKER

No prob.  I am using SMO
You say you don't get any errors.
Are you sure the file "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\DTDB.mdf" doesn't exist?  If it exists when you run this program it will not execute the code. Sorry but I have to start somewhere....
Avatar of vwalla

ASKER

I get an error if I use SqlRestore, but no errors if I use SqlRestoreAsync.
I am positive the DB does not exist.  I detahced and deleted it from the folder.
 
Thanks.
What is the errot you get with Restore?
Avatar of vwalla

ASKER

Restore failed for Server 'OWNER-PC\SQLEXPRESS'
I just saw this, which may be the problem:
{"Access is denied due to a password failure RESTORE DATABASE is terminating abnormally."}
When I backed it up I set a password...I prob need to try and not setting the password on the .bak file?
Avatar of vwalla

ASKER

I spelled the password wrong....crap.
I will award full pojnts for all of you assistance.
ASKER CERTIFIED SOLUTION
Avatar of 13598
13598
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vwalla

ASKER

If someone happens to get their hands on the .bak file, how good is the password at preventing someone from attaching it to a server?  
Lol. It happens  =)
If you are concerned that could happen then yes password protected. You need the password to be able to restore or attach it.
Avatar of vwalla

ASKER

Thanks!  I will take that into consideration.
I work with full SQL (not express). But just go ahead and try to restore it manually without password. It should error out.
Avatar of vwalla

ASKER

It does.  The only drawback of SQL express is some exporting tools that make life easier & the 4GB DB limit.  It's perfect for small apps though for the $$ (or lack there of) :)
Thanks again!