Restoring SQL 2005 Database
Hi All,
I'm trying to restore a database (16GB) through my application. The cmd.BeginExecuteNonQuery completes and returns statistics after running for approx 20 minutes but according to SSMS, the database is still being restored even after restarting the PC.
Code for restoring DB:
vb Code:
Private Sub btnRestoreDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestoreDB.Click
Me.RichTextBoxResult.Clear()
If ValidateInput(Me.GroupBox4) = False Then
Dim RestoreSQL As String = "USE master RESTORE DATABASE [" & Me.ComboBoxDBToRestore.Text & _
"] FROM DISK = N'" & FileToSaveFolderPath & "\" & FileToSaveFileName & "' " & _
"WITH FILE = 1, MOVE N'FilePD' TO N'" & Me.txtboxUserDataLoc.Text & "\" & Me.ComboBoxDBToRestore.Text & ".mdf', " & _
"MOVE N'PD_log' TO N'" & Me.txtboxUserTrnLoc.Text & "\" & Me.ComboBoxDBToRestore.Text & "_Log.ldf', " & _
"REPLACE"
If MessageBox.Show("Are you sure you want to RESTORE the " & Me.ComboBoxDBToRestore.Text & " database?", "Restore Database", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.RichTextBoxResult.Text = ExecuteSQLStmt(RestoreSQL)
Me.btnSetDBParameters.Enabled = True
Me.btnUpdateViews.Enabled = True
End If
Else
MessageBox.Show("Database to Restore / User Data Location / User Transaction Location must be selected...", "Restore Database", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
vb Code:
Public Function ExecuteSQLStmt(ByVal sqlStmt As String)
Dim Result As String = String.Empty
Dim ConnectionString As String = GetConnectionString()
SQLConnection = New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand(sqlStmt, SQLConnection)
cmd.CommandTimeout = 0
SQLConnection.FireInfoMessageEventOnUserErrors = True
SQLConnection.StatisticsEnabled = True
' Open the connection
If SQLConnection.State = ConnectionState.Open Then
SQLConnection.Close()
End If
Using SQLConnection
Try
Dim statistics As IDictionary = SQLConnection.RetrieveStatistics()
Dim bytesReceived As Long = CLng(statistics.Item("BytesReceived"))
Dim bytesSent As Long = CLng(statistics.Item("BytesSent"))
Dim IduRows As Long = CLng(statistics.Item("IduRows"))
Dim ConnectionTime As Long = CLng(statistics.Item("ConnectionTime"))
Dim ExecutionTime As Long = CLng(statistics.Item("ExecutionTime"))
Dim Count As Integer = 0
SQLConnection.Open()
Dim SQLresult As IAsyncResult = cmd.BeginExecuteNonQuery()
While Not SQLresult.IsCompleted
Result = "Waiting " & Count
Threading.Thread.Sleep(100)
Count += 1
Application.DoEvents()
End While
Result += Environment.NewLine & Environment.NewLine & _
"**********Statistics For The Curious**********" & Environment.NewLine & Environment.NewLine & _
"Total Counters: " & statistics.Count.ToString() & Environment.NewLine & _
"Connection Time: " & ConnectionTime.ToString() & Environment.NewLine & _
"Execution Time: " & ConnectionTime.ToString() & Environment.NewLine & _
"Bytes Received: " & bytesReceived.ToString() & Environment.NewLine & _
"Bytes Sent: " & bytesSent.ToString() & Environment.NewLine & _
"Rows Affected: " & IduRows.ToString()
Catch ae As SqlException
Result = ae.Message.ToString()
Catch ae As InvalidOperationException
Result = ae.Message.ToString()
Catch ae As Exception
Result = ae.Message.ToString()
End Try
End Using
Return Result
End Function