Jump to content
Xtreme .Net Talk

How to restore SQL Server database


Recommended Posts

Guest ahmeddc
Posted

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

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...