Solved

Add Sql Database with new name using SMO

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

863 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

25 Experts available now in Live!

Get 1:1 Help Now