Sql Server Database Backup From VB

Sql Server Database Backup From VB

Post by macleod » Tue, 15 Feb 2000 04:00:00



Arun,
You can use the SQLMAINT utility that ships with SQL 7.0 and call it from a VB
program using the ADO command object.
Here is a sample.

    Dim objCnn As New ADODB.Connection
    Dim objCmd As New ADODB.Command
    Dim strQuery As String
    strQuery = "exec master..xp_cmdshell " & "'" & "sqlmaint -S <server> -U
""<user>"" -P "<password>" -D <database-name> -BkUpDB -BkUpMedia DISK
-UseDefDir" & "'" ', no_output" & "'"

    objCnn.Open "Provider=sqloledb.1;data source=macs;user id=sa;password=;"
    objCmd.ActiveConnection = objCnn
    objCmd.CommandType = adCmdText
    objCmd.CommandText = strQuery
    objCmd.Execute

Substitute your server, user id, password and database name in the string
strQuery. This code will  back up your
database in the folder defined as the default for your sql server backups.


> Hi,

> From my VB6.0 application i would like to backup the Sql Server database. I
> cannot do direct copy, because it would say access is denied. Can anybody
> tell me of how to do it.

> Thanks in advance
> Arun

 
 
 

Sql Server Database Backup From VB

Post by Wagner Maxse » Tue, 15 Feb 2000 04:00:00


Great example !
Can you show us how to do the Restore ?
Thanks
Wagner Maxsen



>Arun,
>You can use the SQLMAINT utility that ships with SQL 7.0 and call it from a VB
>program using the ADO command object.
>Here is a sample.

>    Dim objCnn As New ADODB.Connection
>    Dim objCmd As New ADODB.Command
>    Dim strQuery As String
>    strQuery = "exec master..xp_cmdshell " & "'" & "sqlmaint -S <server> -U
>""<user>"" -P "<password>" -D <database-name> -BkUpDB -BkUpMedia DISK
>-UseDefDir" & "'" ', no_output" & "'"

>    objCnn.Open "Provider=sqloledb.1;data source=macs;user id=sa;password=;"
>    objCmd.ActiveConnection = objCnn
>    objCmd.CommandType = adCmdText
>    objCmd.CommandText = strQuery
>    objCmd.Execute

>Substitute your server, user id, password and database name in the string
>strQuery. This code will  back up your
>database in the folder defined as the default for your sql server backups.