Solved

Add Sql Database with new name using SMO

Posted on 2012-03-20
1
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 43

Accepted Solution

by:
Eugene Z 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

717 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