SQL Database Backup Restore Utility
VB.NET Code
Code :
'Database resourecs
Dim con As SqlConnection
Dim cmd As SqlCommand
Shared conn As New SqlConnection
#Region "DATABASE BACKUP"
Public Function DatabaseBackup(ByVal ToDisk As String) As Boolean
Try
con = New SqlConnection(APPCONSTRING)
cmd = New SqlCommand("backup database " & SQL_DBNAME & " to disk='" & ToDisk & ".bak'", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
'MsgBox("Database Backup Successfull")
Catch ex As SqlException
MsgBox("SQlServer Exception :" & ex.Message)
Dim frm As New FrmApplicationSettings
frm.ShowDialog()
Return False
Catch ex As Exception
MsgBox("Exception :" & ex.Message)
Dim frm As New FrmApplicationSettings
frm.ShowDialog()
Return False
Finally
If Not (con Is Nothing) Then con.Dispose()
con = Nothing
If Not (cmd Is Nothing) Then cmd.Dispose()
End Try
Return True
End Function
#End Region
#Region "DATABASE RESTORE"
Public Function DatabaseRestore(ByVal FromDisk As String) As Boolean
Try
MsgBox(FromDisk)
Dim sqlstr As String = "RESTORE DATABASE " & SQL_DBNAME & " " _
& "FROM DISK='" & FromDisk & "'" _
& " WITH RECOVERY"
MsgBox(sqlstr)
Dim CONSTR = "Persist Security Info=False;User ID=" & SQL_USER_ID & ";pwd=" & SQL_PASSWORD & ";Initial Catalog=master;Data Source=" & SQL_SERVER_NAME
con = New SqlConnection(CONSTR)
cmd = New SqlCommand(sqlstr, con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
MsgBox("Database restore Successfull", MsgBoxStyle.Information, MSG_TITLE)
Catch ex As SqlException
MsgBox("SQlServer Exception :" & ex.Message, MsgBoxStyle.Critical, MSG_TITLE)
Return False
Catch ex As Exception
MsgBox("Exception :" & ex.Message, MsgBoxStyle.Critical, MSG_TITLE)
Return False
Finally
If Not (con Is Nothing) Then con.Dispose()
con = Nothing
If Not (cmd Is Nothing) Then cmd.Dispose()
cmd = Nothing
End Try
Return True
End Function
#End Region