Packaging and automating the installation of SQL databases

Source: Internet
Author: User
Tags execution file system include reference create database install node
Packaging | data | database | Automatically installs the needs of a netizen and corrects the MVP Lee Honggen ". NET platform, the OSQL usage error in the deployment (Installation database and autoconfiguration) of the Web application has been tested.

i). To create a deployment project
1. On the File menu, point to Add Item, and then select New Project.
2. In the Add New Project dialog box, select Install and deploy projects in the Project Types pane, and then select Setup Project in the Templates pane. In the Name box, type Setup1.
3. Click OK to close the dialog box.
4. The project is added to Solution Explorer and the File System Editor opens.
5. In the Properties window, select the ProductName property and type the information management system.

Ii. Add the output of the main program project to the deployment project
1. In the File System Editor, select Application Folder. On the Action menu, point to Add, and then select Project Output.
2. In the Add Project Output Group dialog box, select your programs in the Project Drop-down list.
3. Click OK to close the dialog box.
4. Select the primary output and content files groups from the list, and then click OK.

Iii. Creating an Installer class
1. On the File menu, point to New, and then select Project.
2. In the New Project dialog box, select Visual Basic project in the Project Types pane, and then select Class Library in the Templates pane. In the Name box, type Installdb.
3. Click Open to close the dialog box.
4. From the Project menu, choose Add New Item.
5. In the Add New Item dialog box, select Setup class. In the Name box, type Installdb.
6. Click OK to close the dialog box.
7. Detailed code appended.

Iv. Creating a Custom Installation dialog box
1. Select the "Setup1" project in Solution Explorer. On the View menu, point to Editor, and then select User interface.
2. In the User Interface Editor, select the startup node under install. On the Action menu, select Add dialog box.
3. In the Add Dialog dialog box, select the License Agreement dialog box, and then click OK to close the dialog box.
4. In the Add Dialog dialog box, select the text Box (A) dialog box, and then click OK to close the dialog box.
5. On the Action menu, select Move Up. Repeat this step until the text box (A) dialog box is above the installation folders node.
6. In the Properties window, select the BannerText property and type: Install the database.
7. Select the BodyText property and type: Setup will install the database on the target machine
8. Select the Edit1Label property and type: Database name:
9. Select the Edit1Property property and type CUSTOMTEXTA1
10. Select the Edit1Value property and type: Dbservers
11. Select the Edit2label property and type: Server name:
12. Select the Edit2property property and type CUSTOMTEXTA2
13. Select the Edit2value property and type: (local)
14. Select the Edit3label property and type: User name:
15. Select the Edit3value property and type: SA
16. Select the Edit3property property and type CUSTOMTEXTA3
17. Select the Edit4label property and type: password:
18. Select the Edit4property property and type CUSTOMTEXTA4
19. Select the Edit2Visible, Edit3Visible, and Edit4Visible properties and set them to True

V. Create a custom action
1. Select the "Setup1" project in Solution Explorer. On the View menu, point to Editor, and then select Custom Actions.
2. Select the Install node in the Custom Action Editor. On the Action menu, select Add Custom action.
3. In the Select Item in Project dialog box, double-click Application Folder.
4. Select the main output from INSTALLDB (active) Item, and then click OK to close the dialog box.
5. In the Properties window, select the CustomActionData property and type "/dbname=[customtexta1]/server=[customtexta2]/user=[customtexta3]/pwd=[ CUSTOMTEXTA4]/targetdir= "[targetdir]\]".

Attached:/targetdir= "[targetdir]\" is the target path after installation, in order to obtain the installed path in the Installdb class, we set this parameter.

VI). adding files
1. Backup SQL Server to file DB.dat add to the "Setup1" Project (right-click the database in Enterprise Manager-> all work-> back up the database, back up to a file, named DB.dat)
2. Add the installation file Lisencefile.rtf to the "Setup1" project
3. In the User Interface Editor, select the license agreement and set the Lisencefile property to Lisencefile.rtf file
4. Automatically add dependencies to "detected dependencies", and if not, we will manually include them in step 5.
CRYSTAL_MANAGED2003.MSM (if there is a crystal report)
DOTNETFXREDIST_X86.MSM (. NET must be necessary)
... (if there is a reference to another DLL)
5. If the Crystal Report is used, manually add the files to include: Project--> add--> merge module (add your program files) (including dotNetFramework and MDAC27), located at: C:\Program Files\Common Files \merge modules\, * for the necessary
The specific functions are as follows:
(Managed component MSM handles distribution of all managed components, including Windows Forms Viewer, Web Forms Viewer, and all Crystal decisions namespaces)
* CRYSTAL_MANAGED2003.MSM
Crystal_managed2003_chs.msm
(For all other files required to run the report, the database access MSM handles its distribution.) These include database, export, and chart drivers. )
* CRYSTAL_DATABASE_ACCESS2003.MSM
Crystal_database_access2003_chs.msm
(KeyCode MSM processing Crystal decisions Key number installation, note that add merge module, otherwise there is no "mergemouduleproperties" attribute)
* CRYSTAL_REGWIZ2003.MSM
(If the report file uses a Ado.net DataSet DataSet object, the Vc_user_crt71_rtl_x86_---. MSM and vc_user_stl71_rtl_x86_ MSM modules must also be included in the installation works.) And the "Module Retargetable folder" Item of the file installation properties of these two modules must be modified to become the system directory.
Vc_user_crt71_rtl_x86_---. MSM
Vc_user_stl71_rtl_x86_---. MSM
(many people often appear query error, may add this)
5. Open the Solution--> right click on the Crystal_regwiz2003.msm attribute, "License key" in "Mergemouduleproperties" Fill in: Aap5gks0000gde100ds (this is the password that you generate the registration number for the crystal. )


(vii). Add Uninstall function when packing:
Method One:
1. Add file msiexec.exe (usually found under c:\windows\system32\) in the package item
2. Select an application folder in the file system view, press the right button on the Msiexec.exe, select the Create shortcut, and rename the shortcut to uninstall.
3. Change the arguments of this shortcut to "/x {Product ID}" and the value of the product ID is ProductCode The value of the package.
Method Two: (recommended)
1. In the installation package, write down the ProductCode (select Solution Explorer root such as Setup1, and then view the property label, not the property in the right key), use the following
2. Use Vs.net to build a new console program Uninst.exe file
' Power By:landlordh
' For 2000,xp,2003
Module Uninstall
Sub Main ()
Dim myprocess as Process = New process
If System.Environment.OSVersion.ToString.IndexOf ("NT 5") Then
Myprocess.start ("msiexec", "/x{2b65d4a9-c146-4808-ab4b-321fb0779559}") ' changed to its own ProductCode
End If
Myprocess.close ()
End Sub
End Module
3. Add the exe file of the console program Bin directory to the package program file and create a Uninst.exe shortcut in the program group


Report:
Installdb.vb class, to add a reference System.configuration.install.dll:

Imports System.ComponentModel
Imports System.Configuration.Install

<runinstaller (True) > Public Class Installer1
Inherits System.Configuration.Install.Installer

#Region "Component Designer generated code"

Public Sub New ()
MyBase.New ()

' This call is required by the Component Designer.
InitializeComponent ()

' Add any initialization after the InitializeComponent () call

End Sub

' Installer overrides dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub

' Required by the Component Designer
Private Components as System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Component Designer
' You can use the Component Designer to modify this procedure.
' Do not use the Code Editor to modify it.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()
components = New System.ComponentModel.Container
End Sub

#End Region


Public Overrides Sub Install (ByVal statesaver as System.Collections.IDictionary)
Mybase.install (statesaver)
If not INSTALLDB () Then
' Failed, anti-installation
Me.uninstall (statesaver)
Exit Sub
End If
DeleteFile (String.Format ("{0}db.dat", Me.Context.Parameters.Item ("Targetdir"))
End Sub

Public Overrides Sub Uninstall (ByVal statesaver as System.Collections.IDictionary)
' Perform an anti-installation
Mybase.uninstall (statesaver)
DeleteFile (String.Format ("{0}db.dat", Me.Context.Parameters.Item ("Targetdir"))
End Sub

Private Sub DeleteFile (ByVal paths as String)
' Delete the specified file
Try
Dim Delfile as New System.IO.FileInfo (paths)
If delfile.exists Then
Delfile.delete ()
End If
Catch ex as Exception
End Try
End Sub

Private Sub createsql (ByVal paths as String)
Dim File as System.IO.StreamWriter
Dim db as String = String.Format ("{0}", Me.Context.Parameters.Item ("dbname"))
Dim path as String = String.Format ("{0}", Me.Context.Parameters.Item ("Targetdir"))
Try
Dim s as New System.Text.StringBuilder
S.append ("Use master" & VbCrLf)
S.append ("" & VbCrLf)
S.append ("If Not EXISTS" (SELECT * from sysdatabases where name= ' "& db &" ') "& VbCrLf)
S.append ("BEGIN" & VbCrLf)
S.append ("CREATE DATABASE" & DB & VbCrLf)
S.append ("End" & VbCrLf)
S.append ("" & VbCrLf)
S.append ("If exists (SELECT * from sysdevices where name= ' Dbdisk ')" & VbCrLf)
S.append ("BEGIN" & VbCrLf)
S.append ("EXEC sp_dropdevice ' Dbdisk '" & vbCrLf)
S.append ("End" & VbCrLf)
S.append ("Else" & VbCrLf)
S.append ("BEGIN" & VbCrLf)
S.append ("EXEC sp_addumpdevice ' Disk", ' Dbdisk ', ' "& Path &" DB.dat ' "& vbCrLf)
S.append ("End" & VbCrLf)
S.append ("" & VbCrLf)
S.append ("RESTORE Database" & DB & VbCrLf)
S.append ("From disk= '" & Path & "DB.dat '" & vbCrLf)
S.append ("with Replace")
File = New System.IO.StreamWriter (paths)
File.write (s.tostring)
Catch ex as Exception
Finally
File.close ()
End Try
End Sub

Private Function installdb () as Boolean
' Install the database and invoke the automatic batch process.
Try
' Create a temporary script
Createsql (String.Format ("{0}mydb2000tp.sql", Me.Context.Parameters.Item ("Targetdir"))
' Invoke osql execution script
Dim sqlprocess as New System.Diagnostics.Process
SqlProcess.StartInfo.FileName = "Osql.exe"
SqlProcess.StartInfo.Arguments = String.Format ("-u {0}-p {1}-s {2}-I {3}mydb2000tp.sql", Me.Context.Parameters.Item (" User "), Me.Context.Parameters.Item (" pwd "), Me.Context.Parameters.Item (" Server "), Me.Context.Parameters.Item (" Targetdir "))
SqlProcess.StartInfo.WindowStyle = Processwindowstyle.hidden
Sqlprocess.start ()
Sqlprocess.waitforexit () ' Waiting for execution
Sqlprocess.close ()
' Delete script file
DeleteFile (String.Format ("{0}mydb2000tp.sql", Me.Context.Parameters.Item ("Targetdir"))
Return True
Catch ex as Exception
Return False
End Try
End Function

End Class



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.