Click here to Skip to main content
Click here to Skip to main content

.NET Installer that Automatically Installs SQL 2005 Express

By , 28 Jul 2008
 

Introduction

A programmer often has problem like:

  1. Setup and Deployment that contains SQL Express 2005 can only install with default database instance name (SQLEXPRESS), where programmer often wants a different instance name. This is impossible by using Setup and Deployment project.
  2. When the database is installed, some installations need the programmer to manually tell where the backup database is, so it can restore, or mostly programmer do it manually using Management Studio.
  3. Restore often has trouble because the original MDF LDF location and restore destination are different, statements like:
        "[Microsoft][ODBC SQL Server Driver][SQL Server]Directory lookup for the file 
        failed with the operating system error 5(Access is denied.)."
        or
        File 'XXX_Log' cannot be restored to 'x:\yyy'. Use WITH MOVE to identify a valid 
        location for the file. 

    The first error often occurs when the SQL running restore tries to look for path in another SQL database instance (usually it is the default instance) and since it does not have permission to the folder, it generates Access is denied. The second error is when the original MDF location and restore MDF location are different.

  4. Programmer wants a setup file, that installs .NET, Windows installer, SQL Express all automatically.

    This project solves all troubles like the above, enables a Setup file to install needed applications without user knowing how to create database, to restore, to configure, the connection string, and so on, and the application can do it all automatically.

Steps

  1. Download the sample project, and extract it to for example D:\Software Pekerjaan\Makanan.
  2. Download SQL 2005 Express Edition from http://download.microsoft.com/download/e/a/4/ea4b699b-bec4-4722-96d3-254580ed7f9e/SQLEXPR32.EXE Copy it to any folder, and from Command Line (CMD.exe), go to the folder that contains the file, and run "SQLEXPR32.exe /x" and a window will show up asking where to extract the files. Choose a location.
  3. Copy the extracted files and folders to MenuMakanan Project's bin\debug\SQLEXPR2005 folder, the folder already contains a file named Food_Restore.bak which is the database backup programmer created, it must be put there.
  4. Now we will use the InstallationSQLEXPR class from the main Form that shows up in programmer application, for example programmer has a startup form name Login.cs, change the constructor as follows:
     public Login()
            {
                InitializeComponent();
                
                InstallationSQLEXPR _ins = new InstallationSQLEXPR("Food", "JOHN",
                "MenuMakanan", "Food", "JOHNKENEDYDAL", "FOOD_RESTORE.BAK");
                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:

  1. First is Food, which is the application name that shows in your installation progress
  2. 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.
  3. Third is MenuMakanan, which is the new database instance that will be installed, by using Setup and Deployment Project, we cannot name a different database instance other than SQLExpress, but with this InstallationSQLEXPR class we can do it, and we can even specify a sa password by ourselves.
  4. Fourth is Food, which is the database name that the application installs.
  5. Fifth is JOHNKENEDYDAL, which is the sa password for the newly installed SQL EXPRESS, this password is required when you use Management Studio to connect to the database instance.
  6. Food_Restore.BAK is your database backup file name, that is stored in MenuMakanan project bin\debug\SQLEXPR2005 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 Project

  1. Create a new Setup and Deployment project, in this sample already exists name SetupMakananSqlExpr.
  2. In the application folder, create a new folder named SQLEXPR2005, select all files extracted from Step 2 (previous section) and drop to this newly created folder.
  3. Add your database backup file, in this example it is Food_Restore.bak (from MenuMakanan bin\debug\SQLEXPR2005 folder, because the Installation class will look for the backup file in this folder when restoring it. After step two and three, it will look like:

    create_setup.JPG

  4. Now right click SetupMakanan project and select properties, then click Prerequisites button:

    set_prerequisites.JPG

  5. And then select the second radio button, download prerequisites from the same location in my application. Click OK, and save.

  6. Build your whole solution, and then build SetupMakananSqlExpr project, and you will see in SetupMakananSqlExpr 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.

When user installs the application, it will install Windows Installer and .NET if it does not exist, and when you first run your application, it checks if the database instance exists, if not it will install the database instance, and restore the backup database to the new database instance.

Notes

  1. RestoreDatabase method in library can restore to database though the backup original MDF location is different to restore one (different database instance/PC)
  2. Library has JohnKenedy.DataAccess namespace and must be included to programmer Windows Form project
  3. SQL 2005 Express cannot be installed with different database instance other than SQLExpress, but this library can do it because of using ProcessStartInfo and install it when application finished install and first run
  4. SQLEXPR32.exe provided by Microsoft must be extracted (with /x option) in order to be able to install using command line arguments, which this library uses
  5. The installation process of SQL 2005 Express can be seen by user, but user just clicks next and watches the progress bar without configuring it since it is configured by the arguments passed by the library.

History

This library is not fully ready for Installation, instead the library has Data Access feature - the article can be found at http://www.codeproject.com/kb/library/johnkenedy_library.aspx.

I have created another article that installs MSDE and attaches database to MSDE too, you can read about it at http://www.codeproject.com/KB/dotnet/_Net_Installer_With_MSDE.aspx?
msg=2654769#xx2654769xx.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

John Kenedy S.Kom
Software Developer (Senior)
Singapore Singapore
Member
I write code mostly in C#, VB.NET, PHP and Assembly.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionError in installationmemberamish kumar1 Oct '12 - 19:47 
hi jhon
how to sort out this error plz help me
 
"Module C:\Program Files\JOHN KENEDY\setupMakananSqlExpr\SQLEXPR2005\Setup\ProgramFiles\Microsoft SQL Server\x86\Binn\xmlrw.dll failed to register. HRESULT -2147024769. Contact Your Support Personel".
QuestionDoubt in deployment as well as installationmemberPunithaSabareeswari10 Aug '12 - 0:56 
hi i had create .net application which uses a database (Sql server 2005).
know i want to create and deploy my application on a system, where sql server is not install. can i deploy and use my application on that without installing the sql server?
 
if possible means how..
Questiondeploy an database applicationmemberSalmanqazi6 Jul '12 - 11:55 
hi i had create .net application which uses a database (Sql server 2008).
know i want to create and deploy my application on a system, where sql server is not install. can i deploy and use my application on that without installing the sql server?
QuestionHow to create a setupfile in C# windows applicationmemberNirmala Devi Saravanan30 Mar '12 - 23:33 
hi,
 
Iam developing in c# with SQL Server 2005 windows Application
 
Iam using the below
 
1.Bussiness Logic Layer
2.Data Access Layer
3.Facet- All Declartaion of Variables for above Layer
4.WinUI- All forms,Reports,Exe are here in this folder
4.1. Bin Folder
4.1.1 Debug
4.1.1 Reports Folder
4.1.2 WinUi.exe
 

I tried to create a SetUp File for the above.
But since WINUI exe is in Different folder i couldnt create it.
So please kindly help me to create a setup file for the above with sql server installer
 
U can also contact me thru nirmala.nisha@gmail.com
 

Regards
Nirmala Saravanan
Bugreusing your codememberIanArhip22 Mar '12 - 5:06 
I have imported your library in my code and I called the installer with the code you suggested and I got this error:
Retrieving the COM class factory for component with CLSID {10020100-E260-11CF-AE68-00AA004A34D5} failed due to the following error: 80040154.
 
I appreciate you putting in an effort to help out us folks, but your code is too big to make sense quickly. I gave up after half hour and i will be implementing my own solution. I am better off with a solution that I can control over a solution that may be better but is behaving eratically.
QuestionPlease help ::: Failed to add the classesmemberkfsehgal20 Feb '12 - 4:29 
Hi,
I am trying to follow the steps you have given in your above article. I need to use it in Vb.net. I have added the SQLExpress files and .Bak files as you have suggested.
But when I put the code in the startup form of my application,
Dim _ins As New InstallationSQLEXPR("MyTestInstallation", "localhost", "MyOwnDBInstance", "dbMyOwnDBName", "FOOD_RESTORE.BAK")
If _ins.IsDone = False Then
_ins.ShowDialog()
End If
If _ins.IsRestart = True Then
Application.[Exit]()
Me.Close()
Return
End If
it returns error on InstallationSQLEXPR (not declared) I understand I need to add class but how?
Please help........
 
Thanks a lot
QuestionWebSetupmemberSwapan Sardar20 Jan '12 - 2:56 
Dear Kom,
 
I have MSSQL Server(2005) database and Asp.net 2 web application and it will running fine in developer machine. I want to develop a web setup which will install in another pc only click on the next button and finish (to create a setup file that user just click next2 and will install .net framework, sql express and the application connect to the database, restore the app database to it, all automatically and now with SQL Express ) .
 
I am waiting for your kind response.
 
Thanks and Regards.
 
Swapan Sardar
E-mail: sardar_swapan@rediffmail.com
QuestionChange bootstrappermemberafilipovik31 Oct '11 - 6:04 
Here is how to do it with changing the bootstrapper:
http://es.narkive.com/2006/9/6/1058725-proyecto-de-implementacion-de-aplicacion-con-base-de-datos.html[^]
QuestionhellomemberMember 830647419 Oct '11 - 5:34 
can i use sql server 2008 express edition instead of sql server 2005?
Questionplease help mememberrdssiva31 Mar '11 - 23:08 
Dear John,
 
Please find the below error msg.. am getting this error msg while configure the db..
 

 

 
---------------------------
 
---------------------------
System.Runtime.InteropServices.COMException (0x80040000): [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied.
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()).
 
at SQLDMO.SQLServerClass.Connect(Object ServerName, Object Login, Object Password)
 
at JohnKenedy.DataAccess.BusinessSQLEXPRInstaller.IsDatabaseExist() in C:\Inetpub\wwwroot\Makanan2\JohnKenedy.DataAccess\BusinessSQLEXPRInstaller.cs:line 126
 
at JohnKenedy.DataAccess.InstallationSQLEXPR.btnAttach_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\Makanan2\JohnKenedy.DataAccess\InstallationSQLEXPR.cs:line 143
 
at System.Windows.Forms.Control.OnClick(EventArgs e)
 
at System.Windows.Forms.Button.OnClick(EventArgs e)
 
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
 
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
 
at System.Windows.Forms.Control.WndProc(Message& m)
 
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
 
at System.Windows.Forms.Button.WndProc(Message& m)
 
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
 
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
 
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
 
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
 
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
 
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
 
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
 
at System.Windows.Forms.Application.RunDialog(Form form)
 
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
 
at System.Windows.Forms.Form.ShowDialog()
 
at MenuMakanan.Login..ctor() in C:\Inetpub\wwwroot\Makanan2\MenuMakanan\Login.cs:line 22
 
at MenuMakanan.Program.Main() in C:\Inetpub\wwwroot\Makanan2\MenuMakanan\Program.cs:line 20
---------------------------
OK
---------------------------
 

 
Please give solution to resolve this problem.
 
Thanks & Regards,
S. Siva Kumar

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 28 Jul 2008
Article Copyright 2008 by John Kenedy S.Kom
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid