The VB.NET-MySQL Tutorial - Part 7
Introduction
In our previous article we added the final base functionality to our application, allowing for the creation and use of custom status messages. Now that we have a basic working application we can move to creating an installer for our application, allowing it to be deployed to other PCs.
There are multiple options for creating installers for a Visual Basic application, including the new one-click installer that will be included with Visual Studio.NET 2005. In this tutorial I am going to introduce you to an excellent Open Source installer called InnoSetup. InnoSetup is a script-driven installer, written in Delphi, that is very versatile and expandable. InnoSetup is free for commercial use and is available for download at www.innosetup.com.
In addition to the files generated by the Visual Basic compiler, we also need the .NET framework 2.0 installer, for those machines that do not have the appropriate version of the .NET framework installed. The .NET framework redistributable package can be downloaded here.
Building the Application
Before we can package the application into an installer, we need to build an executable. Open your project in VB.NET and choose the Clean option from the Build menu. The Clean option deletes all binary and intermediate files from the project, ensuring that the next build will not contain any old files. After the clean is completed, choose the Build option from the Build menu.
After building the project, the bin directory will contain the In-Out.exe executable. and the In-Out.exe.config configuration file. As these are the only two files in our application the installation will be relatively simple; we must install the two built files and run the .NET framework installer.
Creating an Installation Support Directory
First we need to create an installation support directory within our project directory to store the installer files and the support files. Add a \install directory with \support and \output sub-directories to your project directory.
Move the .NET framework installer (dotnetfx.exe) into the \support directory. The \output directory will contain the compiled install file.
Creating the Install Script
Now that we have built the executable. and prepared the support files, we are ready to create the application installer. The InnoSetup tool comes with a wizard interface that we will use to build the basic install profile. Download InnoSetup from www.innosetup.com and install it before proceeding.
Once you have downloaded and installed InnoSetup, start InnoSetup and you will be presented with a file dialog:

Choose the Create a new script file using the Script Wizard option and click OK.
The next dialog is an introduction to the wizard:

Make sure the Create a new empty script file option is un-checked and click Next >.
Setting the Application Information
The next dialog in the wizard is for setting some basic application information:

Set the appropriate application information for your project and click Next > (if you are unsure about a piece of information just use a dummy value, you can change it later).
Setting the Program Directory
After setting the basic application information, we can configure the installation directory for our application:

It is generally best to install to the Program Files directory of the target machine. Set the application directory name and click Next >.
Specifying Installation Files
The next step is to specify the files that will be included with the installer:

Browse to the executable file you created in VB.NET and specify it as your application main executable file. After identifying your main executable, use the Add file(s) button to add the .config file for your executable.
After you have specified your application files, click Next >.
Creating Shortcuts and Icons
After specifying installation files we move on to creating a Start menu entry and various shortcuts to our application:
![]()
Select a start menu folder name (usually your application name) and choose your desired options. I like to add an Uninstall icon to the start menu entry just to make things more convenient for my users.
Once you have configured the Start menu entry, click Next >.
Specifying Install Documentation
The next screen of the wizard allows you to specify installation documentation files:

Of particular interest is the License file; if you specify a license file, the user will be unable to proceed with the installation without agreeing to the contents of the license file.
In our case we do not have a license file, so leave the fields blank and click Next >.
Specifying Output Files
The final step in the InnoSetup wizard is to specify where the output files of the InnoSetup compiler will be placed:

Set the Custom compiler destination base directory to the \output subdirectory you created previously. You can change the base name of your installer if you wish, in my case I have changed the name to in-out-setup (InnoSetup will take care of the extension).
After entering your output settings, click Next >.
Finishing the InnoSetup Wizard
On the final screen of the InnoSetup wizard click the Finish button. When prompted, click the No button to skip compiling the new script. You should be presented with a setup script similar to this one:
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
[Setup]
AppName=In-Out
AppVerName=In-Out 1.0
AppPublisher=Mike Hillyer
AppPublisherURL=http://www.openwin.org
AppSupportURL=http://www.openwin.org
AppUpdatesURL=http://www.openwin.org
DefaultDirName={pf}\In-Out
DefaultGroupName=In-Out
OutputDir=C:\Documents and Settings\mhillyer\Desktop\in-out\install\output
OutputBaseFilename=in-out-setup
Compression=lzma
SolidCompression=yes
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
Name: "quicklaunchicon"; Description: "{cm:CreateQuickLaunchIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
[Files]
Source: "C:\Documents and Settings\mhillyer\Desktop\in-out\In-Out\bin\In-Out.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Documents and Settings\mhillyer\Desktop\in-out\In-Out\bin\In-Out.exe.config"; DestDir: "{app}"; Flags: ignoreversion
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
[Icons]
Name: "{group}\In-Out"; Filename: "{app}\In-Out.exe"
Name: "{group}\{cm:UninstallProgram,In-Out}"; Filename: "{uninstallexe}"
Name: "{userdesktop}\In-Out"; Filename: "{app}\In-Out.exe"; Tasks: desktopicon
Name: "{userappdata}\Microsoft\Internet Explorer\Quick Launch\In-Out"; Filename: "{app}\In-Out.exe"; Tasks: quicklaunchicon
[Run]
Filename: "{app}\In-Out.exe"; Description: "{cm:LaunchProgram,In-Out}"; Flags: nowait postinstall skipifsilent
This script is enough to perform a basic installation of the application in a target machine. Choose the Save option of the File menu of InnoSetup and save the script in the /install directory of your project (I named mine in-out.iss). You may want to change the various file paths in the script to relative paths, just to make it easier to move your project directory around.
Adding the .NET Framework
While the installer would now successfully install our application, we need to add support for installing the .NET Framework. The dotnetfx.exe we downloaded earlier needs to be added to the installer and run as part of the installation process. This will radically increase the size of the installation (the MSI installer is 22 megabytes), but it cannot be avoided since we cannot assume that the target machine will have the .NET Framework 2.0.
Adding a File to the Install Script
We need to instruct InnoSetup to add dotnetfx.exe to the installer. We will want to have this file available during the installation as a temporary file that is deleted at the end of the installation.
Files that are part of the installation are listed in the [Files] section of the installer script. We specify a source and destination for each file, along with special flags that dictate the behavior of the file. In our case the source is support/dotnetfx.exe, the destination is a special target called {tmp} that maps to a temporary directory on the target machine:
Source: support\dotnetfx.exe; DestDir: {tmp}; Flags: deleteafterinstall
This line ensures that the MSI installer for the .NET Framework will be available to the installer.
Executing the Installer
Now that the installer is part of the installation package, we need to add a call to the installation script to run it before the installation completes.
We can run applications during the install process by adding them to the [Run] section of the install script. We can specify parameters to pass to the application, along with a working directory and a status message to display while the application runs:
Filename: {tmp}\dotnetfx.exe; Parameters: "/q"; WorkingDir: {tmp}; StatusMsg: Installing DotNET Framework 2.0
The parameter we are passing to dotnetfx.exe instructs it to be quiet during the installation and thus not bother the user as much.
Adding Connector/NET
The last file needed for our application is the assembly file for Connector/NET, named MySql.Data.dll. The assembly file can be found in the Connector/NET installation directory (typically C:\Program Files\MySQL\MySQL Connector NET 1.0.X).
Copy the dll file to your \Support directory and add the following line to your install script:
Source: support\MySql.Data.dll; DestDir: {app}
This will add the dll file to your application install directory.
Compiling the Installer
Now that we have added the .NET Framework and Connector/NET to the installer, we can proceed with the install. Before we do, here is an example of what your install script may look like:
[Setup]
AppName=In-Out
AppVerName=In-Out 1.0
AppPublisher=Mike Hillyer
AppPublisherURL=http://www.openwin.org
AppSupportURL=http://www.openwin.org
AppUpdatesURL=http://www.openwin.org
DefaultDirName={pf}\In-Out
DefaultGroupName=In-Out
OutputDir=output
OutputBaseFilename=in-out-setup
Compression=lzma
SolidCompression=yes
[Tasks]
Name: desktopicon; Description: {cm:CreateDesktopIcon}; GroupDescription: {cm:AdditionalIcons}; Flags: unchecked
Name: quicklaunchicon; Description: {cm:CreateQuickLaunchIcon}; GroupDescription: {cm:AdditionalIcons}; Flags: unchecked
[Files]
Source: ..\In-Out\bin\In-Out.exe; DestDir: {app}; Flags: ignoreversion
Source: ..\In-Out\bin\In-Out.exe.config; DestDir: {app}; Flags: ignoreversion
Source: support\dotnetfx.exe; DestDir: {tmp}; Flags: deleteafterinstall
Source: support\MySql.Data.dll; DestDir: {app}
[Icons]
Name: {group}\In-Out; Filename: {app}\In-Out.exe
Name: {group}\{cm:UninstallProgram,In-Out}; Filename: {uninstallexe}
Name: {userdesktop}\In-Out; Filename: {app}\In-Out.exe; Tasks: desktopicon
Name: {userappdata}\Microsoft\Internet Explorer\Quick Launch\In-Out; Filename: {app}\In-Out.exe; Tasks: quicklaunchicon
[Run]
Filename: {tmp}\dotnetfx.exe; Parameters: "/q"; WorkingDir: {tmp}; StatusMsg: Installing DotNET Framework 2.0
Filename: {app}\In-Out.exe; Description: {cm:LaunchProgram,In-Out}; Flags: nowait postinstall skipifsilent
You can begin compiling the installer by choosing the Compile option from the Build menu of InnoSetup. The progress of the compile will be displayed in the bottom window of InnoSetup. Once the compile is complete we can test the installer.
Testing the Installer
To test the installer, choose the Run option from the Run menu of InnoSetup. As you work through the installer debug information will be presented in the bottom window of InnoSetup. If your installer works without error during this test, the next step will be to test the installation on a ‘clean’ machine that you have not installed your application on before and which does not have the .NET Framework installed.
If your application works properly on the clean machine you can be fairly certain that it will run properly on the target platform represented by that machine. If you expect to deploy on other versions of Windows, you may want to try installing your application on clean machines running that version of Windows.
Improving the Installer
There are some minor improvements that could be made to our installer, including the addition of support for downloading the .NET Framework files only if needed, and adding different install types such as Minimal, Full, and Custom. While these changes will not be covered in this article, the InnoSetup documentation is quite good and should help you make such changes if you so desire.
Conclusion
We now have a working installer for our application, allowing us to distribute the application to our users. The installer deploys our application, the .NET Framework, and Connector/NET, allowing the application to be deployed on any number of desktops, all with a versatile, Open Source installer that ships a single executable. for installation.
The source code for this project is available at http://www.vbmysql.com/wp-content/uploads/vb-mysql-tutorial-part-7.zip.
This will be the last regular article in this tutorial series, I hope it has proven of use to you in learning the basics of working with Visual Basic.NET and MySQL. As I continue development of the application covered in these articles, I will write individual articles covering single subjects such as the system tray, creating configuration screens, and using version control.
November 7th, 2006 at 7:01 pm
Nice articles, thanks! really helped me!
November 17th, 2006 at 7:49 am
Testing out a new app… got program working just how i want it… only problem is if the end user has .net already installed they have to select either “repair” or “remove” any idea on how i could check to see if they have .net and give them the right file reguardless?
mike
January 2nd, 2007 at 2:13 am
I can’t connect to my database. It says that it can’t connect, which is true because it’s trying to connect to my ip instead of the one I specified.
February 25th, 2007 at 6:27 am
The reference to the MySQL client library doesn’t work, because the reference is an absolute path (and you can’t make it relative).
February 25th, 2007 at 6:45 am
Nevermind. It happened to be some images that I was loading in at runtime.
February 26th, 2007 at 1:12 am
Thank you for this tutorial, it was a great series. The only thing I don’t see in this is the installation of mysql and then creating the database with tables and any data that might be in the database. Is there another tutorial here at vbmysql.com that explains how to do that. I think I can figure out how to install mysql with the InnoSetup script but the creating the database and the tables is a little to much for me at this point in my education. Any help in this would be appreciated.
Thanks
Jamie
February 26th, 2007 at 7:19 pm
About the creating the database: you have to make another program that creates the databases and tables and run it before the installation ends. That’s how i am doing with the updates of the programs, but should work just fine and with the creation, or manual create them.
February 26th, 2007 at 7:24 pm
Is there a tutorial on how to go about doing that? Are you talking about a vb.net program to do that? And do I have to run the mysql 5.0 installation first?
Thanks for the help.
Jamie
March 27th, 2007 at 10:46 pm
Good tutorial, thnks a lot!!!!
March 28th, 2007 at 1:56 pm
For Jamie Holcom: i don’t know if there is a tutorial about how to do that, but it can be done, and yes, with a vb.net program or directly in your program - i have done one that . here’s some code
Dim conn1 = New MySqlConnection()
conn1.ConnectionString = “server=localhost;” & _
“user id=root;” & _
“password=” & ********
‘”;
‘Try
Dim myCommand As New MySqlCommand
Dim mySQLstr As String
Dim nume As String
Dim dialog As New OpenFileDialog
If dialog.ShowDialog = System.Windows.Forms.DialogResult.OK Then
nume = dialog.FileName.ToString
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(nume)
Try
mySQLstr = fileContents
conn1.Open()
myCommand.Connection = conn1
myCommand.CommandText = mySQLstr
myCommand.ExecuteNonQuery()
conn1.Close()
MsgBox(”Baza de date a fost creata!”)
Catch myerror As MySqlException
MsgBox(”Eroare: ” & myerror.Message)
Finally
If conn1.State ConnectionState.Closed Then conn1.Close()
End Try
End If
it will open a dialog to chose the sql file containing the structure
of course it can be automated.
ATTENTION: Do not specify the database in the connection string.
March 28th, 2007 at 2:02 pm
I am working at an installation that installs everything - without user interaction - like instantrails (google it), it’s not such a big deal but helps deploying applications made with mysql.
June 5th, 2007 at 8:07 am
great tutorial.. really help me for my projekt.. i’m working with vb.net since 2weeks and i had really problems with mysql.. thx for this
July 26th, 2007 at 5:26 pm
I haven’t had a complete chance to read all the way through, but it seems very complete. My question is how different is it to connect to a MySQL on an external web server? I would like to build an application which needs a db, and there are constant updates. I would like to just update the db, and have the users always have the most up to date info without needing to download. So my plan is to create a VB.net 2k5 app with a MySQL on my website. Hopefully I am not dreaming.
July 27th, 2007 at 6:16 pm
itsamemario - to use mysql remotely for updates you will need to use the root account. What you want to do it can be done
good luck!
July 30th, 2007 at 7:06 am
Thanks for the tutorial, however, one major problem is when developing the application, a certain password is used with the root account, and that is what is available in the code. So how can mysql be installed together with the custom password to the root account?
July 30th, 2007 at 8:41 pm
if my aplication does have crystal reports and needs dlls that are in the common files. when developing in others pcs dont work do yo know what i must do ?
August 23rd, 2007 at 9:58 am
Hello all . . .
I need some help . . .
I have create a database in MySQL and I’m using the VB.NET 2005 to manage my data
When i try to input or update some records in the database tables instead of Greek characters that I’m writing I’m getting back question marks (?)
I have change the table and the column character sets and still the problem is there ? ? ?
What can i do for that ? ? ?
Thanks a lot . . .
October 17th, 2007 at 3:48 pm
Awesome articles, thank you for being so comprehensive in your writing.
March 14th, 2008 at 1:18 pm
This has been the most thorough tutorial I have ever come across.
You have provided everything that I needed to get me started, and this last article has helped to close the loop.
Thank you very much!
Mike
June 18th, 2008 at 9:13 am
great tutorial! thanks! hope there will be more!