Hi to all vb.net community!
In my application,the user may be required to install SQLExpress2008WT. The User Interface is as attached.
The following steps are performed.
a) Browse button browses the .exe file path (via a file dialog) and displays it.
b) The Installation button does what it says, in the following order.
-Extract the installation files.
-Install Sql (Quietly).
-Attach a certain scripted database to Database Engine.
I have tested and it installs good.
Problem:
While running, I want the label (lblmsg) to be blinking (using a timer say) and displaying at what stage
the installation is. e.g
"Extracting Files..." then "Installing Sql..." then "Creating and Attaching Database...".
All examples I see online perform some looping. But an installation like this one is a synchronous process.
What should be the go around?
I think that this can be achieved by threading or background worker I have never used these objects. But how?
Extract, Install, Attach are all in one method below.
Sub Extract_SQL_Files_To_Desktop_And_Install()
MyString = "Extracting installation files !!!"
lblMsg.ForeColor = Color.Green
lblMsg.Text = MyString
Dim Desk As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim strDirectory As String = Desk & "\SQL Files"
'delete files in the root folder SQL Files
For Each d In Directory.GetDirectories(strDirectory)
Directory.Delete(d, True)
Next
For Each f In Directory.GetFiles(strDirectory)
File.Delete(f)
Next
'copy files to root folder SQL Files
My.Computer.FileSystem.CopyDirectory(Application.StartupPath & "\All DBScripts", strDirectory & "\All DBScripts", True) 'copy dbsystem scripts
'start extracting SQL files
Me.Cursor = Cursors.WaitCursor
Dim temppath As String = lblPath.Text
Dim processObj As Process = Process.Start(temppath, " /Q /X:C:\SQL2008")
Do While Not processObj.WaitForExit(1000)
'refresh process informaton
processObj.Refresh()
Loop
My.Computer.FileSystem.CopyDirectory("C:\SQL2008", strDirectory)
Directory.Delete("C:\SQL2008", True)
MyString = "Installing SQL Express !!!"
lblMsg.ForeColor = Color.Blue
lblMsg.Text = MyString
Dim processStartInfo As System.Diagnostics.ProcessStartInfo
processStartInfo = New System.Diagnostics.ProcessStartInfo()
processStartInfo.FileName = strDirectory & "\setup.exe"
If System.Environment.OSVersion.Version.Major >= 6 Then ' Windows Vista or higher
processStartInfo.Verb = "runas"
End If
processStartInfo.Arguments = " /QUIET=""TRUE"" /q /Action=Install /Hideconsole /Features=SQL,Tools /InstanceName=""SQLEXPRESS"" /SQLSYSADMINACCOUNTS=""BUILTIN\ADMINISTRATORS"" /SQLSVCACCOUNT=""NT AUTHORITY\NETWORK SERVICE"" /BROWSERSVCSTARTUPTYPE=""Automatic"" /SECURITYMODE=""SQL"" /SAPWD=""xxxxxxxx"" /TCPENABLED=1 /NPENABLED=1"
processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
processStartInfo.UseShellExecute = True
processObj = System.Diagnostics.Process.Start(processStartInfo)
ProgressBar1.Value = 0
Do While Not processObj.WaitForExit(1000)
processObj.Refresh()
If ProgressBar1.Value = 100 Then
ProgressBar1.Value = 0
End If
ProgressBar1.Value += 1
Loop
If processObj.ExitCode = 0 Then
ProgressBar1.Value = 0
End If
MyString = "Creating DBSystem database !!!"
lblMsg.Text = MyString
'count .sql files in a folder
Dim filepath As String = strDirectory & "\All DBScripts"
Dim dirInfo As New System.IO.DirectoryInfo(filepath)
Dim intCount As Integer = dirInfo.GetFiles("*.sql").Count
'run each script
For i As Integer = 0 To intCount - 1
Dim ScriptName As String = "scr" & i & ".sql"
If i = 0 Then
Dim ConStr As String = "Persist Security Info=false;Initial Catalog=master;Data Source=xxxxx\sqlexpress;User ID=sa;Password=xxxxxx;"
Dim Con As New SqlConnection(ConStr)
Dim serva As Server = New Server(New ServerConnection(Con))
Dim f As New FileInfo(filepath & "\" & ScriptName)
Dim script As String = f.OpenText().ReadToEnd()
serva.ConnectionContext.ExecuteNonQuery(script)
Else
Dim ConStr As String = "Persist Security Info=True;Initial Catalog=DBSystem;Data Source=xxxxx\sqlexpress;User ID=sa;Password=xxxxxxx;"
Dim Con As New SqlConnection(ConStr)
Dim serva As Server = New Server(New ServerConnection(Con))
Dim f As New FileInfo(filepath & "\" & ScriptName)
Dim script As String = f.OpenText().ReadToEnd()
serva.ConnectionContext.ExecuteNonQuery(script)
End If
ProgressBar1.Value += 1
Next
ProgressBar1.Value = 100
MyString = "SQL Express and DBSystem database installed successfully!!!"
lblMsg.Text = MyString
ProgressBar1.Value = 0
ProgressBar1.Hide()
Me.Cursor = Cursors.Default
End Sub