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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

13598Commented:
Try adding this line before res.ReplaceDatabase = True  :
res.LastRestore = True
0
vwallaAuthor Commented:
LastRestore is not a member of...
0
13598Commented:
nvm,I thought you were using  SQLDMO
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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
13598Commented:
If you don't need to I would go away with it. Do you need to have a password?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.