Solved

Add Sql Database with new name using SMO

Posted on 2012-03-20
1
190 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

773 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