Solved

Copy SQL Server db from VB

Posted on 2004-08-16
3
206 Views
Last Modified: 2010-05-02
Hi,
Is there a way to copy SQL Server database, give this copy different name and connect to this new db as needed. Everything has to be done from VB. Code samples will be appreciated.
Thanks.
0
Comment
Question by:andr
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
hamood earned 500 total points
ID: 11810192

This example uses both the BACKUP and RESTORE statements to make a copy of the Northwind database. The MOVE statement causes the data and log file to be restored to the specified locations. The RESTORE FILELISTONLY statement is used to determine the number and names of the files in the database being restored. The new copy of the database is named TestDB. For more information, see RESTORE FILELISTONLY.

Create stored procedure with these statements

BACKUP DATABASE Northwind
   TO DISK = 'c:\Northwind.bak'
RESTORE FILELISTONLY
   FROM DISK = 'c:\Northwind.bak'
RESTORE DATABASE TestDB
   FROM DISK = 'c:\Northwind.bak'
   WITH MOVE 'Northwind' TO testdb.mdf',
   MOVE 'Northwind_log' TO testdb.ldf'
GO

call stored procedure and now you can connect with that database

Hamood
0
 
LVL 4

Expert Comment

by:hamood
ID: 11810747
here is stored proc code in northwind data

CREATE PROCEDURE SP_Test  AS

      BACKUP DATABASE Northwind
   TO DISK = 'c:\Northwind.bak'
RESTORE FILELISTONLY
   FROM DISK = 'c:\Northwind.bak'
RESTORE DATABASE Testing2
   FROM DISK = 'c:\Northwind.bak'
   WITH MOVE 'Northwind' TO 'c:\Testing2.mdf',
   MOVE 'Northwind_log' TO 'c:\Testing2.ldf'
GO

here is vb6 code

Private Sub Command1_Click()
Set mCommand = New ADODB.Command
     Dim con As New ADODB.Connection
    con.open "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=CT1"
       
    With mCommand
         .ActiveConnection = con
            .CommandTimeout = 300
            .CommandText = "SP_Test"
            '.Parameters.Append(ID As Integer)
            '.CreateParameter(ID as ResID) 'Also tried this
            .Execute
        End With
        Set mCommand = Nothing
End Sub

0
 

Author Comment

by:andr
ID: 11811137
Thanks, that works. Only little modification:
instead of
WITH MOVE 'Northwind' TO 'c:\Testing2.mdf'
needed
WITH MOVE 'Northwind_dat' TO 'c:\Testing2.mdf',

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

776 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