Restore database

You can use this code to restore database into SQL server
  1. Private Sub BtnRestore_Click(sender As Object, e As EventArgs) Handles BtnRestore.Click
  2.  Try
  3.           Dim  database = CmbDatabase .Text
  4.             Dim ServrnameRestore = CmbServer .Text .Trim
  5.             Dim OpenFile1 As New OpenFileDialog
  6.             Dim Constr As String
  7.             Constr ="Data Source=" & ServrnameRestore &"; Initial Catalog=master; Integrated Security=True"
  8.             'Open connection
  9.             Dim sqlcon As SqlConnection
  10.              Dim cmd As SqlCommand
  11.             If OpenFile1 .ShowDialog = DialogResult.OK Then
  12.                 sqlcon = New SqlConnection(Constr)
  13.                 sqlcon.Open()
  14.                ' OpenFileDialog1.ShowDialog()
  15.                 filename_Restore = OpenFile1 .FileName
  16.                Dim  StrQuery2 ="Restore Database " &  database  &" from disk ='" & filename_Restore  &"' WITH  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 5"
  17.                 'Excute query
  18.            
  19.                 cmd = New SqlCommand (Strquery2 ,sqlcon)
  20.                 cmd.ExecuteNonQuery ()
  21.                 MessageBox .Show ("Database "  & database  & " has been restored successfull","Message", MessageBoxButtons .OK, MessageBoxIcon .Information )
  22.                 sqlcon.Close ()
  23.                
  24.              Else
  25.                 MsgBox ("Cancel",MsgBoxStyle.Information ,"Cancel")
  26.             End If
  27.  
  28.         Catch ex As Exception
  29.             MessageBox .Show (ex.Message ,"Message", MessageBoxButtons .OK , MessageBoxIcon .Warning )
  30.         End Try
  31. End sub
Thank you follow Instagram: hassan_hambe

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment