?
Solved

Add Sql Database with new name using SMO

Posted on 2012-03-20
1
Medium Priority
?
218 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 2000 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

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

770 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