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:
  1. Private Sub btnRestoreDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestoreDB.Click
  2.  
  3.         Me.RichTextBoxResult.Clear()
  4.  
  5.         If ValidateInput(Me.GroupBox4) = False Then
  6.  
  7.             Dim RestoreSQL As String = "USE master RESTORE DATABASE [" & Me.ComboBoxDBToRestore.Text & _
  8.             "] FROM  DISK = N'" & FileToSaveFolderPath & "\" & FileToSaveFileName & "' " & _
  9.             "WITH  FILE = 1,  MOVE N'FilePD' TO N'" & Me.txtboxUserDataLoc.Text & "\" & Me.ComboBoxDBToRestore.Text & ".mdf', " & _
  10.             "MOVE N'PD_log' TO N'" & Me.txtboxUserTrnLoc.Text & "\" & Me.ComboBoxDBToRestore.Text & "_Log.ldf', " & _
  11.             "REPLACE"
  12.  
  13.             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
  14.                 Me.RichTextBoxResult.Text = ExecuteSQLStmt(RestoreSQL)
  15.                 Me.btnSetDBParameters.Enabled = True
  16.                 Me.btnUpdateViews.Enabled = True
  17.             End If
  18.         Else
  19.             MessageBox.Show("Database to Restore / User Data Location / User Transaction Location must be selected...", "Restore Database", MessageBoxButtons.OK, MessageBoxIcon.Error)
  20.         End If
  21.  
  22.     End Sub




vb Code:
  1. Public Function ExecuteSQLStmt(ByVal sqlStmt As String)
  2.  
  3.         Dim Result As String = String.Empty
  4.  
  5.         Dim ConnectionString As String = GetConnectionString()
  6.         SQLConnection = New SqlConnection(ConnectionString)
  7.         Dim cmd As New SqlCommand(sqlStmt, SQLConnection)
  8.         cmd.CommandTimeout = 0
  9.  
  10.         SQLConnection.FireInfoMessageEventOnUserErrors = True
  11.         SQLConnection.StatisticsEnabled = True
  12.  
  13.         ' Open the connection
  14.         If SQLConnection.State = ConnectionState.Open Then
  15.             SQLConnection.Close()
  16.         End If
  17.  
  18.         Using SQLConnection
  19.             Try
  20.  
  21.                 Dim statistics As IDictionary = SQLConnection.RetrieveStatistics()
  22.                 Dim bytesReceived As Long = CLng(statistics.Item("BytesReceived"))
  23.                 Dim bytesSent As Long = CLng(statistics.Item("BytesSent"))
  24.                 Dim IduRows As Long = CLng(statistics.Item("IduRows"))
  25.                 Dim ConnectionTime As Long = CLng(statistics.Item("ConnectionTime"))
  26.                 Dim ExecutionTime As Long = CLng(statistics.Item("ExecutionTime"))
  27.                 Dim Count As Integer = 0
  28.  
  29.                 SQLConnection.Open()
  30.  
  31.                 Dim SQLresult As IAsyncResult = cmd.BeginExecuteNonQuery()
  32.                 While Not SQLresult.IsCompleted
  33.                     Result = "Waiting " & Count
  34.                     Threading.Thread.Sleep(100)
  35.                     Count += 1
  36.                     Application.DoEvents()
  37.                 End While
  38.  
  39.                 Result += Environment.NewLine & Environment.NewLine & _
  40.                 "**********Statistics For The Curious**********" & Environment.NewLine & Environment.NewLine & _
  41.                 "Total Counters: " & statistics.Count.ToString() & Environment.NewLine & _
  42.                 "Connection Time: " & ConnectionTime.ToString() & Environment.NewLine & _
  43.                 "Execution Time: " & ConnectionTime.ToString() & Environment.NewLine & _
  44.                 "Bytes Received: " & bytesReceived.ToString() & Environment.NewLine & _
  45.                 "Bytes Sent: " & bytesSent.ToString() & Environment.NewLine & _
  46.                 "Rows Affected: " & IduRows.ToString()
  47.             Catch ae As SqlException
  48.                 Result = ae.Message.ToString()
  49.             Catch ae As InvalidOperationException
  50.                 Result = ae.Message.ToString()
  51.             Catch ae As Exception
  52.                 Result = ae.Message.ToString()
  53.             End Try
  54.         End Using
  55.         Return Result
  56.     End Function