Introduction
Often programmers want to build a single installer package that can install the application along with prerequisites and database. Fortunately for database, programmers can use MSDE which can be distributed along with your application, but MSDE lacks user interface so to configure it often we need to use command line.
Scenario
This is the scenario:
- A programmer wants a single package that automatically installs .NET Framework and then installs MSDE and uses the database for the application that a programmer writes.
- In this tutorial, the installation will create a new database instance, with a name specified by the programmer, and it can detect whether this instance exists and as for removal, so for safety programmer just create a unique database instance name.
- The library then automatically attaches the database to this newly installed database instance. First I have written two projects which are required to be included in your solution. First is
JohnKenedy.DataAccess
and second is JohnKenedy.DataAccessModule
. This is because the installation code is coming along with this library. - The goal of this project is, user gets the installation files, double clicks the EXE, and clicks next and next following the instruction without the need to understand how to connect database, the database name, username, password and so on, user just clicks next and next, the application will install the .NET prerequisites, the database, and attach the database that programmer has created all automatically, and the application that programmer writes, runs and connects to the database without user knowing how to configure them. What user needs to know is only how to use the application that the programmer writes.
Steps
- Download the sample code I attach and extract it to D:\Software Pekerjaan\Makanan (I use for this example.)
- Assume the
MenuMakanan
project is the Windows Application project that you are creating and which is going to be deployed. From this project, add reference to library named JohnKenedy.DataAccess
.
- Download MSDE by clicking here. Double click the .exe and by default it will extract to your C:\sql2ksp3. Copy the extracted contents to
MenuMakanan
project bin\debug\MSDE2000 folder. - Detach your application's database to become MDF file, and copy the MDF file only to
MenuMakanan
project bin\debug\MSDE2000 folder. For this example, it is Food_Data.MDF which is contained in the download for example. - In your application (
MenuMakanan
), add an Application Configuration file or app.config (in this sample, it already exists), and create two new keys in AppSettings
section, read App.Config in sample for more information. Remember to set IsInstall=0
, and IsRestart=0
as follows, because this will tell your application to install before running, IsRestart
will be 1
if it is finished installing and requires to restart your computer so that the newly installed MSDE service is running.
<add key="IsInstall" value="1"/>
<add key="IsRestart" value="0"/>
- In your startup Form or form that is first run when application starts in this example is Login.cs, in the constructor after
InitialilzeComponent
, add codes that look like this:
public Login()
{
InitializeComponent();
Installation _ins = new Installation(
"Food", "JOHN", "MenuMakanan", "Food",
"JOHNKENEDYDAL", "FOOD_Data.MDF");
if (_ins.IsDone == false) _ins.ShowDialog();
if (_ins.IsRestart == true)
{
Application.Exit();
this.Close();
return;
}
}
Remember to add using JohnKenedy.DataAccess;
so that Installation class can be found. This Installation class accepts 6 parameters, which I explain below:
- First is
Food
, which is the application name that shows in your installation progress. - Second is
JOHN
, which is your computer name, you can just simply use "(local)" to replace "JOHN" to say that the installation is done to the computer running Setup. - Third is
MenuMakanan
, which is the new database instance that will be installed, this is similar with (local)\SQLExpress database instance that Microsoft installed when we install VS 2005, though this one is MSDE one. This name must be unique, and this database instance will only contain one database name which will be defined in the fourth parameter, and be used by your application only. - Fourth is
Food
, which is the database name that the application installs. - Fifth is
JOHNKENEDYDAL
, which is the sa password for the newly installed MSDE, this password is required when you use Enterprise Manager (SQL 2000) to connect to the database instance. - Food_Data.MDF is your MDF file name, that is stored in
MenuMakanan
project bin\debug\MSDE2000 folder. You can change this as it suits you.
As you notice, there will be code to check IsDone
and IsRestart
, left both of this checking as it is, since it is the condition that Installation
class left for us to tell the main Startup Form what to do, whether to close the application or to show the form.
Now compile the application. Your compiled MenuMakanan.exe will automatically install your application because the configuration is IsInstall=1
and IsRestart=0
, and if the installation succeeds, the value will be both 0
which is automatically set by the application and so the application will just start normally.
Create Setup and Deployment Package
- Create a new Setup and Deployment project, in this sample it already exists with name
SetupMakanan
. - In the application folder, create a new folder name MSDE2000, and inside this folder create three folder names Msi, MSM, and Setup. Then inside MSM folder, create a folder names 1033. This structure is the same with the MSDE extracted folder earlier in Step 3, we then add files to these folders MSDE2000, Msi, MSM, 1033, and Setup folder according to the MSDE extracted folders.
- Add Food_Data.MDF to MSDE2000 folder, because the
Installation
class will look for the filename in this folder. After step two and three, it will look like:
Click here for a bigger image
- Now right click
SetupMakanan
project and select properties, then click Prerequisites button:
And then select the second radio button. Download prerequisites from the same location in my application. Click OK, and save.
- Build your whole solution, and then build
SetupMakanan
project, and you will see in SetupMakanan
project debug folder Setup.exe SetupMakanan.msi, and two new folders, one is Windows Installer 3_1 and another is DotNetFX35 which are the .NET prerequisites.
Now you can ship these files to a CD or zip it for distribution, the Setup application will install prerequisites and then your application to the folder. And when user runs your application, it checks the IsInstall
and IsRestart
parameters to determine whether database installation is needed, if yes, then it will install it for user and then ask for restart and after restarting it will check again whether to attach database or not, if yes then it will attach, and finally it will run your application.
History
This library is not fully for Installation, instead the library has Data Access feature, which article can be found here.
Installation is one of the features in the library I developed recently. I have tested the Setup package in a computer that never had .NET and database installed, and it did successfully install .NET and MSDE and run the application smoothly.