Solved

Copy SQL Server db from VB

Posted on 2004-08-16
3
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month7 days, 15 hours left to enroll

617 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