Solved

Copy SQL Server db from VB

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

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

17 Experts available now in Live!

Get 1:1 Help Now