Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Backup/Restore database using SQL-DMO

Posted on 2002-03-27
3
Medium Priority
?
724 Views
Last Modified: 2007-12-19
Hi, all,

I am using SQL-Server 2000, VC++6.0, SQL-DMO and I am trying to backup/restore a database. Here is what I need:

1. Backup a database, saying A. Then then I got a file say FileA.

2. Create a database B.

3. "Restore" database B based on FileA on the SAME SQLServer. Basically, I want everything in database B is exactly the same as database B.

The problem I got is SQL-DMO complains that the Data file of A (..\Microsoft SQLServer\MSSQL\DATA\A_Data.MDF) is being used.

The key point here:

1. In the backup file. I assume it keeps the data file name A_Data.MDF, A_Log.Ldf, ...
2. Since I need to "restore"(or I should say create) the database B from backup file A in the same SQL server, but File A is still being used, that causes problem.
3. If there is a way somehow rename A_Data.MDF, A_Log.Ldf in the backup file, then I guess the problem can be solved.

Any hints ? Please help.

Thanks.




0
Comment
Question by:mikechen
3 Comments
 

Expert Comment

by:Cool2k
ID: 6901417
Setting the property DatabaseFiles of Restore/backup object may help you. I never tried.

I am also interested in knowing the answer.

Magesh
0
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 800 total points
ID: 6903353
You need to look at the RelocateFiles property.  This tells DMO where, and with what name, the files are to be restored.

Here's a VB example:

Option Explicit
Dim oSQLDMOSQLServer
Dim oSQLDMORestore

Set oSQLDMOSQLServer = CreateObject("SQLDMO.SQLServer")
oSQLDMOSQLServer.LoginSecure = True
oSQLDMOSQLServer.Connect "MyServer"

Set oSQLDMORestore = CreateObject("SQLDMO.Restore")
oSQLDMORestore.Database = "Northwind2"
oSQLDMORestore.Files = "C:\Backups\NorthwindBackup.bak"
oSQLDMORestore.RelocateFiles = _
    "[Northwind]" & "," & "[c:\temp\Northwind2.mdf]" & "," & _
    "[Northwind_log]" & "," & "[c:\temp\Northwind2.ldf]"

oSQLDMORestore.SQLRestore(oSQLDMOSQLServer)
0
 

Author Comment

by:mikechen
ID: 6907750
Thank you for your information.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

783 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