Bagaimana caranya menginstal database ketika deploying sebuah aplikasi?

Created at : Feb/22/2007 02:06:30   [Click this link to download code sample]
1306 Views   4 Comments

Bagaimana caranya menginstal database ketika deploying sebuah aplikasi? .net framework sudah menyediakan class yang dapat digunakan untuk melakukan hal tsb. Dengan menggunakan Class Installer pada Class Library project template kita dapat dengan "mudah" melakukannya...
Dalam class installer terdapat prosedur-prosedur yang harus kita override code implementasinya, prosedur tsb yaitu : Install, Uninstall, Commit, Rollback..."Mereka" lah yang bertugas untuk eksekusi script database. Begitu juga kalau terjadi error on the way maka prosedur Rollback yang akan dijalankan. 
Sudah pasti kita harus membuat Setup Project template nya untuk menjalankan Class Installer yang dibuat. Pada setup project tsb ditambahkan Custom Actions editor yang akan mengeksekusi script database setelah main aplikasinya beres diinstal terlebih dahulu. Ok...berikut langkah-langkahnya :

1. - Buat sebuah Class Library project template. 
    - Hapus file Class1 yang ditambahkan secara default.
    - Tambahkan Class Installer (Add New Item dari projectnya)
    - Tambahkan code dibawah ini :

C# code :  

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.ComponentModel;
   4:  using System.Configuration.Install;
   5:   
   6:  namespace InstallerClassLib
   7:  {
   8:      [RunInstaller(true)]
   9:      public partial class DBInstaller : Installer
  10:      {
  11:          public DBInstaller()
  12:          {
  13:              InitializeComponent();
  14:          }
  15:          public override void Install(
  16:              System.Collections.IDictionary stateSaver)
  17:          {
  18:              base.Install(stateSaver);
  19:   
  20:              //File Path ini didapatkan dari 
  21:              //setting CustomActionnya nanti
  22:              string strSqlFilePath =
  23:                  this.Context.Parameters["args"];
  24:   
  25:              //gunakan osql.exe untuk execute 
  26:              //script database yang akan diinstal
  27:              System.Diagnostics.ProcessStartInfo psi =
  28:                  new System.Diagnostics.ProcessStartInfo(
  29:                  "osql.exe", @"-S .\sqldev2k5 -E -i " +
  30:                  Convert.ToChar(34) + strSqlFilePath
  31:                  + Convert.ToChar(34));
  32:              //Convert.ToChar(34) digunakan untuk menghasilkan 
  33:              //apostrope (single quote)
  34:  
  35:              psi.WindowStyle =
  36:                  System.Diagnostics.ProcessWindowStyle.Normal;
  37:              psi.UseShellExecute = false;
  38:  
  39:              try
  40:              {
  41:                  System.Diagnostics.Process proses =
  42:                      System.Diagnostics.Process.Start(psi);
  43:              }
  44:              catch (Exception ex)
  45:              {
  46:                  throw new InstallException(ex.Message +
  47:                      strSqlFilePath);
  48:              }
  49:          }
  50:   
  51:          public override void Uninstall(
  52:              System.Collections.IDictionary savedState)
  53:          {
  54:              base.Uninstall(savedState);
  55:          }
  56:   
  57:          public override void Commit(
  58:              System.Collections.IDictionary savedState)
  59:          {
  60:              base.Commit(savedState);
  61:          }
  62:   
  63:          public override void Rollback(
  64:              System.Collections.IDictionary savedState)
  65:          {
  66:              base.Rollback(savedState);
  67:          }
  68:      }
  69:  }


VB code :

   1:  Imports System.ComponentModel
   2:  Imports System.Configuration.Install
   3:   
   4:  Public Class DBInstaller
   5:   
   6:      Public Overrides Sub Install( _
   7:      ByVal savedState As System.Collections.IDictionary)
   8:          MyBase.Install(savedState)
   9:   
  10:          '//File Path ini didapatkan dari 
  11:          '//setting CustomActionnya nanti 
  12:          Dim strSqlFilePath As String = _
  13:          Me.Context.Parameters.Item("Args")
  14:   
  15:          '//gunakan osql.exe untuk execute 
  16:          '//script database yang akan diinstal
  17:          '//sesuaikan nama instance name server nya 
  18:          '//pada opsi -S .\sqldev2k5
  19:          Dim psi As ProcessStartInfo = New ProcessStartInfo( _
  20:          "osql.exe", "-Slocalhost -E -i " & Chr(34) & _
  21:          strSqlFilePath & Chr(34))
  22:          '//Chr(34) digunakan untuk 
  23:          '//menghasilkan apostrope (single quote) 
  24:   
  25:          psi.WindowStyle = ProcessWindowStyle.Normal
  26:          psi.UseShellExecute = False
  27:          Try
  28:              Dim p As Process = Process.Start(psi)
  29:          Catch e As Exception
  30:              Throw New InstallException(e.Message + _
  31:              strSqlFilePath)
  32:          End Try
  33:      End Sub
  34:   
  35:      Public Overrides Sub Commit(ByVal savedState As _
  36:      System.Collections.IDictionary)
  37:          MyBase.Commit(savedState)
  38:      End Sub
  39:   
  40:      Public Overrides Sub Rollback(ByVal savedState As _
  41:      System.Collections.IDictionary)
  42:          MyBase.Rollback(savedState)
  43:      End Sub
  44:   
  45:      Public Overrides Sub Uninstall(ByVal savedState As _
  46:      System.Collections.IDictionary)
  47:          MyBase.Uninstall(savedState)
  48:      End Sub
  49:   
  50:  End Class


2. Buat sebuah windows application yang digunakan sebagai main applicationnya.

3. Generate sql script dari database yang akan di gunakan. Anda bisa menggunakan enterprise manager (sql server 2000) atau sql server management studio (sql server 2005) tergantung dari jenis database yang digunakan. Pada contoh yang terdapat di file yang dapat anda download ini menggunakan database sql server 2005.

4. Tambahkan Setup Project pada solution, and then do the following steps :
  - Add project output to the Application folder from Class Library (Class Installer) project   and Windows Application project. You can do these at the File System Editor by clicking its toolbar menu above Solution Explorer.
 - Tambahkan file .sql yang sudah di generate dengan klik kanan application folder >> Add >> File...
 - Klik Custom Actions editor di toolbar menu
 - Klik kanan Custom Actions node di Custom Actions editornya >> Add Custom Action >> pilih Primary Ouput dari Class Libary project (Installer Class) dari Application Folder
 - Klik kanan node Primary Output tepat di bawah node Install >> pilih Properties Window
 - assign properti CustomActionData dengan nilai :
/args="[TARGETDIR]RoelDB.sql"
sesuaikan nama file .sql nya. Variabel "args" tsb juga digunakan di Installer Class dan harus sama nama variabelnya...




5. Build all your project

6. Install the application

7. See the results from these screen shot :




[Comments]
Yosafat May/07/2008 04:56:19 :
Pak Rully, Apakah ada contoh untuk install database kalau kita publish aplikasi kita dengan clickonce Thanks
Rully May/07/2008 05:17:14 :
Pak Yosafat, saya belum pernah coba apakah bisa dengan cara Click Once, setau saya ada keterbatasan, nnt klo saya sudah coba saya akan share disini :)
Dipz Feb/01/2010 11:32:29 :
Pak Rully, ini kasusnya jika di PC klien sudah terinstal SQL Server jg kah? Bagaimana cara deployment aplikasi + database SQL Server, namun di PC Klien/User tidak terinstall SQL Server? saya bingung mohon tutorialnya Pak, THanks
Rully Yulian MF Feb/02/2010 07:51:15 :
@Dipz > Betul mas, ini kasusnya apabila SQL Server nya sudah terinstal di PC klien. SQL Server nya memang harus diinstal secara manual atau terpisah.

[Write your comment]
Name (required)
URL (optional)
Example : http://www.yulianmf.com  
Comment

1846
Input code above below (Case Sensitif) :
About Me 
Rully Yulian MF
My Name is Rully Yulian Muhammad Firmansyah. I am an IT Trainer, IT Consultant and Application Developer spesializing in Microsoft .NET technology and SQL Server database. I live in Bandung, Indonesia. My hobby is to play Guitar. [Read More...]
Top Download 
Change Group,Sort Order, Filtering By Date in Crystal Reports : Downloaded 1893 times  
Bagaimana caranya menginstal database ketika deploying sebuah aplikasi? : Downloaded 1834 times  
Simple Voice Engine Application With Sound Player Class... : Downloaded 1230 times  
DataGridView Grouping : Downloaded 1029 times  
WinForms DataGrid Paging With SqlDataAdapter : Downloaded 962 times  
Article Category 
Links 
Award 
Certifications 
MCT
MCPD
MCTS
MCAD.NET
ASP.NET Brainbench
Facebook 
LinkedIn 
Syndication (RSS 2.0) 
Powered By