Solved

Backup/Restore database using SQL-DMO

Posted on 2002-03-27
3
697 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:
bhess1 earned 200 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dynamics crm 2011 8 44
MS SQL Backup 24 70
Getting certain data from a string 1 24
SQL server 2008 SP4 29 33
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

706 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

21 Experts available now in Live!

Get 1:1 Help Now