Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copy SQL Server db from VB

Posted on 2004-08-16
3
Medium Priority
?
211 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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

721 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