Backup SQL Server Database from Visual Basic

Posted on 2005-04-12
Last Modified: 2013-11-25
Hii All ,
I want to give a backup & restore utility for my appliaction .
Application should be able to backup the database
I use VB 6.0 , SQL Server 2000 . SQL Server Anuthentication mode.

My main concern is dump file should be password Protected . So that no one should not be able to restore that File to somewhere & can see my DB Structure


Question by:rahulbagal
    LVL 52

    Assisted Solution

    by:Carl Tawn
    Depending on how your SQL Server is currently setup you will need to run one or two queries.

    If you don't already have a "device" setup for backups then you will need to use the sp_adddumpdevice stored procedure to create one. The syntax for the query can be found here:

    Secondly, or if you already have a dumpdevice set up, you will need to run a "BACKUP DATABASE" query. The syntax for the query can be found here:

    Note that the "BACKUP DATABASE" query requires you to specify the name of a pre-exisiting dumpdevice, or the one created by sp_adddumpdevice. The "BACKUP DATABASE" query also allows you to specify a password for the backup so that nobody else can restore it.

    Hope this helps.
    LVL 1

    Accepted Solution

    I am giving you some code here that backups data base

    Public Function G_F_BackupDatabase(ByVal P_File2Backup As String) As String
       On Error GoTo errorhandler
       Dim L_S_Sql As String
       If gobjCon.G_Connection.State = adStateOpen Then gobjCon.G_Connection.Close
       Set gobjCon = Nothing
       gobjCon.G_Connection.Execute ("USE MASTER")
       If val(L_S_Sql) > 0 Then
             gobjCon.G_Connection.Execute ("EXEC SP_DROPDEVICE 'MyDump'")
       End If
       L_S_Sql = "EXEC SP_ADDUMPDEVICE 'DISK','MyDump','" & P_File2Backup & "'"
       gobjCon.G_Connection.Execute (L_S_Sql)
       L_S_Sql = "BACKUP DATABASE " & gstrDbName & " TO MyDump WITH INIT"
       gobjCon.G_Connection.CommandTimeout = 0
       gobjCon.G_Connection.Execute (L_S_Sql)
       Set gobjCon = Nothing
       G_F_BackupDatabase = retSuccess
       Exit Function
       G_F_BackupDatabase = retFailed & " : " & Err.Description
    End Function

    change the query accoding to
    As suggested by by carl_tawn

    gobjCon is a ADO connection class & G_Connection is Public member of type ADODB.Connection

    LVL 1

    Expert Comment

    P.S. and connectDB method opens connection G_Connection

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    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…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    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…
    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…

    779 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

    11 Experts available now in Live!

    Get 1:1 Help Now