Author: B.Henry 05/08/2021
Language:
Visual Basic .NET
Tags:
Here's a class I wrote to backup an MS SQL database.
Imports System.Data.SqlClient
Public Class MSSQL_Backup
Public SqlConnectionString As String = ""
''' <summary>
''' Backup an SQL Server 2005/2008
''' </summary>
''' <param name="BackupDrive"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function MSSQL_Backup(ByVal BackupDrive As String) As Boolean
Try
If BackupDrive = "" Then
MsgBox("You must add a Backup Location.")
Exit Function
End If
'Reset connect
Dim connect As SqlConnection = Nothing
'Set SQL connection and open it
connect = New SqlConnection(SqlConnectionString)
connect.Open()
'Execute backup query
Dim command As SqlCommand = Nothing
command = New SqlCommand("backup database YourDatabaseName to disk ='" & BackupDrive & "' with init,stats=10", connect)
command.ExecuteNonQuery()
'Close SQL Connection
connect.Close()
connect.Dispose()
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical)
End Try
End Function
End Class