How to restore SQL Server database

  • Thread starter Thread starter ahmeddc
  • Start date Start date
A

ahmeddc

Guest
hi


I used this code to restore the database from a specified path or root path, but the following error occurs

my code


Public conA As New SqlConnection("Data Source=DESKTOP-TRRGQMF\SQLEXPRESS2014; Initial Catalog=namedb; Integrated Security=true")



Try
cmd = New SqlCommand("restore database namedb from disk='C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS2014\MSSQL\Backup\namedb.bak'", conA)

conA.Open()
cmd.ExecuteNonQuery()
conA.Close()

Catch ex As Exception
MsgBox(ex.Message)
End Try

another way

'Try
' Dim database As String = conA.Database.ToString()
' If conA.State <> ConnectionState.Open Then
' conA.Open()

' End If
' Dim sqlStmt2 As String = String.Format("ALTER DATABASE [" + database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE")
' Dim bu2 As SqlCommand = New SqlCommand(sqlStmt2, conA)
' bu2.ExecuteNonQuery()
' Dim sqlStmt3 As String = "USE MASTER RESTORE DATABASE [" + database + "] FROM DISK='" + "E:\namedb.bak" + "' WITH REPLACE"
' Dim bu3 As SqlCommand = New SqlCommand(sqlStmt3, conA)
' bu3.ExecuteNonQuery()
' Dim sqlStmt4 As String = String.Format("ALTER DATABASE [" + database + "] SET MULTI_USER")
' Dim bu4 As New SqlCommand(sqlStmt4, conA)
' bu4.ExecuteNonQuery()
' MessageBox.Show("database restore done successefuly")
' conA.Close()

'Catch ex As Exception
' MsgBox(ex.Message)

'End Try




1625314.png

Continue reading...
 
Back
Top