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

LVL 1
vwallaAsked:
Who is Participating?
 
13598Connect With a Mentor Commented:
If you don't need to I would go away with it. Do you need to have a password?
0
 
13598Commented:
Try adding this line before res.ReplaceDatabase = True  :
res.LastRestore = True
0
 
vwallaAuthor Commented:
LastRestore is not a member of...
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
13598Commented:
nvm,I thought you were using  SQLDMO
0
 
vwallaAuthor Commented:
No prob.  I am using SMO
0
 
13598Commented:
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....
0
 
vwallaAuthor Commented:
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.
0
 
13598Commented:
What is the errot you get with Restore?
0
 
vwallaAuthor Commented:
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?
0
 
vwallaAuthor Commented:
I spelled the password wrong....crap.
I will award full pojnts for all of you assistance.
0
 
vwallaAuthor Commented:
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?  
0
 
13598Commented:
Lol. It happens  =)
0
 
13598Commented:
If you are concerned that could happen then yes password protected. You need the password to be able to restore or attach it.
0
 
vwallaAuthor Commented:
Thanks!  I will take that into consideration.
0
 
13598Commented:
I work with full SQL (not express). But just go ahead and try to restore it manually without password. It should error out.
0
 
vwallaAuthor Commented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.