Need help while working with timer....
I am trying to do the database restoring part....
When the user clicks on the restore database button the progress bar shud be shown and as soon as the restoring is completed the progress bar shud stop and display success msg...
Right now I tried without timer using progress image but it is not working as expected...
the image shows just when the success msg is displayed....
below is my code
Imports Microsoft.Win32
Imports Microsoft.SqlServer.Management.Smo
Imports System.Data.SqlClient
Imports System.IO
Public Class frmDatabaseRestore
Private Sub frmDatabaseRestore_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
lblAlert.Visible = False
lblAlert.Image = My.Resources.Loading1 'my processing image
txtDatabase.Text = "Databasename"
cboServer.Text = "Servername"
End Sub
Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
If MessageBox.Show("Restoring Database Aborted, your system will shutdown", "Restore Database Aborted", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
Application.Exit()
End If
End Sub
Private Sub btnRestore_Click(sender As System.Object, e As System.EventArgs) Handles btnRestore.Click
If optBackupFile.Checked = False And optTextFile.Checked = False Then
MessageBox.Show("Select any one option")
ElseIf optBackupFile.Checked = True Then
If cboServer.Text = "" Or cboServer.Text = "Select" Then
MessageBox.Show("Server Name is Mandatory")
Else
lblAlert.Visible = True 'display the processing image
Try
Dim MySQL_Connection As SqlConnection = New SqlConnection()
MySQL_Connection.ConnectionString = "Data Source = " & cboServer.Text & ";Integrated Security = True"
MySQL_Connection.Open()
Dim myCommand As SqlCommand
myCommand = New SqlCommand(" RESTORE DATABASE [Databasename] FROM DISK = N'D:\Databasename_full.bak'", MySQL_Connection)
myCommand.ExecuteNonQuery()
MySQL_Connection.Close()
lblAlert.Visible = False
MessageBox.Show("Database Restored Successfully")
frmUserLogin.Show()
Me.Close()
Catch ex As Exception
WriteLog(New StackTrace(New StackFrame(True)).GetFrame(0).GetFileLineNumber, ex.Message & "Restore Database Fail")
MessageBox.Show("Restore Database Fail")
End Try
End If
'else retore database based on text files
ElseIf optTextFile.Checked = True Then
If cboServer.Text = "" Or cboServer.Text = "Select" Then
MessageBox.Show("Server Name is Mandatory")
Else
lblAlert.Visible = True
Dim MySQL_Connection As SqlConnection = New SqlConnection()
MySQL_Connection.ConnectionString = "Data Source = " & cboServer.Text & ";Integrated Security = True"
MySQL_Connection.Open()
Dim myCommand As SqlCommand
Dim dbstatus As Boolean
'Creating Database
myCommand = New SqlCommand("CREATE DATABASE Databasename", MySQL_Connection)
myCommand.ExecuteNonQuery()
MySQL_Connection.Close()
Dim myconn As SqlConnection = New SqlConnection
dbstatus = Open_DB_Connection(myconn)
'Creating Tables
myCommand = New SqlCommand("CREATE TABLE Table1 (Col1 int NULL,Col2 int NULL)", myconn)
myCommand.ExecuteNonQuery()
myCommand = New SqlCommand("CREATE TABLE Table2 (Colname1 varchar(1) NOT NULL,Colname2 varchar(25))", myconn)
myCommand.ExecuteNonQuery()
Dim FileName As String
Dim i As Integer = 0
Dim Names_Of_Tables() As String
Dim dbCmd As SqlCommand = New SqlCommand()
Names_Of_Tables = {"Table1", "Table2"}
For i = 0 To Names_Of_Tables.Length - 1
FileName = "D:\" & Names_Of_Tables(i) & ".txt"
If File.Exists(FileName) Then
Debug.Print("Filename: " + FileName)
Debug.Print("Names_Of_Tables: " + Names_Of_Tables(i))
Try
dbCmd = New SqlCommand("BULK INSERT " & Names_Of_Tables(i) & " FROM '" & FileName & "' WITH (FIELDTERMINATOR = '^', ROWTERMINATOR = '\n');", myconn)
Debug.Print("Query: " + dbCmd.CommandText)
dbCmd.ExecuteNonQuery()
MessageBox.Show(Names_Of_Tables(i) & " - Table created successfully")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Else
MessageBox.Show("File " + FileName + " does not exists")
End If
Next
'****************************************************************
dbstatus = Close_DB_Connection(myconn)
lblAlert.Visible = False
MessageBox.Show("Database Restored Successfully")
frmUserLogin.Show()
Me.Close()
End If
End If
End Sub
End Class