Solved

Copy SQL Server db from VB

Posted on 2004-08-16
3
207 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access Search and Replace Using VBA 6 85
Run code from text file in vb 1 73
VB6 - Convert HH:MM into Decimal 8 63
clicking a shape in a frame array vb6 3 47
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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