Solved

Add Sql Database with new name using SMO

Posted on 2012-03-20
1
172 Views
Last Modified: 2014-05-22
Hi,

I'm trying to add a databse from a sql baclup and assign the databse a new name. I'm unable to do it using the following code.

            Svr = New Server(cn)
            Dim TargetDB as string = "NewDB"
            Dim SourceDb as string = "OldDB"
            DirString = Svr.Information.RootDirectory + "\Backup\"
            Dim res As Restore = New Restore()
            res.Devices.AddDevice(DirString + "FUNDEZ_BackupData.bak", DeviceType.File)
            res.Database = TargetDb
            res.NoRecovery = False
            res.ReplaceDatabase = False
                        res.RelocateFiles.Add(New RelocateFile(SourceDB + "_Data", Svr.Information.RootDirectory.ToString + "data\" + TargetDb + ".mdf"))
            res.RelocateFiles.Add(New RelocateFile(SourceDB + "_log",Svr.Information.RootDirectory.ToString + "data\" + TargetDb + ".ldf"))
            res.SqlRestore(Svr)

I always get error "Logical file 'FUNDEZ_backup test 1' is not part of database 'FUNDEZ_backup test 1'. Use RESTORE FILELISTONLY to list the logical file names.
RESTORE DATABASE is terminating abnormally."

Any Help would be appreciated.

Thanks,
0
Comment
Question by:FUNDEZ
1 Comment
 
LVL 42

Accepted Solution

by:
EugeneZ earned 500 total points
ID: 37746434
use this code example:
Getting Started with SMO in SQL 2005 - Restores
http://www.sqldbatips.com/showarticle.asp?ID=40
--

regarding your error:

"Use RESTORE FILELISTONLY to list the logical file names."
to get logical file name and use it in your script:

E.g.

RESTORE FILELISTONLY FROM DISK = '\\yourbkpFilelocationPath\FUNDEZ_BackupData.bakWITH FILE = 1
GO
more:

http://www.mssqltips.com/sqlservertutorial/109/sql-server-restore-filelistonly/

http://msdn.microsoft.com/en-us/library/ms173778.aspx
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now