C # package the project and automatically install the SQL database

Source: Internet
Author: User

1). Create a deployment project
1. Point to "add project" on the "file" menu and select "new project ".
2. In the "Add new project" dialog box, select "Install and deploy Project" in the "project type" pane, and select "Install Project" in the "template" pane ". In the Name box, type setup1.
3. Click OK to close the dialog box.
4. The project is added to Solution Explorer and opened in the file system editor.
5. In the "properties" window, select productname properties and type the information management system.
2) Change the master Program Add project output to deployment project
1. In the file system Editor, select Application folder ". On the "operations" menu, point to "add" and select "project output ".
2. In the "add project output group" dialog box, select "your program" from the "project" drop-down list ".
3. Click OK to close the dialog box.
4. Select the "main output" and "Content File" groups from the list, and click "OK ".
3). Create the installer class
1. Point to "new" on the "file" menu and select "project ".
2. In the "new project" dialog box, select "Visual Basic Project" in the "project type" pane and select "class library" in the "template" pane ". In the "name" box, type installdb.
3. Click Open to close the dialog box.
4. Select "Add new project" from the "project" menu ".
5. In the "Add new project" dialog box, select "Installer class ". In the "name" box, type installdb.
6. Click OK to close the dialog box.
7. Details Code Attached.
4). Create a custom installation dialog box
1. Select the "setup1" project in Solution Explorer. On the "View" menu, point to "Editor" and select "User Interface ".
2. In the User Interface Editor, select the "Start" node under "installation. On the "operations" menu, select "add dialog box ".
3. In the "add dialog box", select the "License Agreement" dialog box and click "OK" to close the dialog box.
4. In the Add dialog box, select the text box (A) and click OK to close the dialog box.
5. On the "operations" menu, select "Move Up ". Repeat this step until the "text box (a)" dialog box is located on the "Install Folder" node.
6. In the "properties" window, select the bannertext attribute and type: Install database.
7. Select the bodytext attribute and type: the installer will install the database on the target machine.
8. Select the edit1label attribute and type: Database Name:
9. Select the edit1property property and type customtexta1
10. Select the edit1value attribute and type dbservers.
11. Select the edit2label attribute and type: Server Name:
12. Select the edit2property property and type customtexta2
13. Select the edit2value attribute and type: (local)
14. Select the edit3label attribute and type: User Name:
15. Select the edit3value attribute and type: SA
16. Select the edit3property property and type customtexta3
17. Select the edit4label attribute and type: password:
18. Select the edit4property property and type customtexta4
19. Select the edit2visible, edit3visible, and edit4visible attributes and set them to true.
5). Create a custom operation
1. Select the "setup1" project in Solution Explorer. On the "View" menu, point to "Editor" and select "Custom operations ".
2. Select the "Install" node in the Custom operation editor. On the "operations" menu, select "add custom operation ".
3. In the "select project items" dialog box, double-click "application folder ".
4. Select "primary output from installdb (activity)" and click "OK" to close the dialog box.
5. in the "properties" window, select the customactiondata attribute and type "/dbname = [customtexta1]/Server = [customtexta2]/user = [customtexta3]/Pwd = [customtexta4]/targetdir =" [targetdir] "".
Appendix:/targetdir = "[targetdir]" is the target path after installation. To obtain the path after installation in the installdb class, we set this parameter.
6). Add files
1. back up SQL server to a file dB. add dat to the "setup1" project (right-click the database in the Enterprise Manager> all jobs> back up the database and back up it into 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 attribute to the lisencefile. rtf file.
4. Generally, dependencies are automatically added to "detected dependencies". If not, manually add them to Step 5)
Crystal_managed2003.msm (if there is a crystal Report)
Dotnetfxredist_x86.msm (. NET must be required)
... (If any other DLL is referenced)
5. if a crystal report is used, manually add the file to be included: Project --> Add --> Merge Module (add your program file) (including dotnetframework and mdac27) at: C: under program filescommon filesmerge modules, * is required
The specific functions are as follows:
(Managed component MSM processes distribution of all managed components, including windows form viewer, web form viewer, and all crystal decisions namespaces)
* Crystal_managed2003.msm
Crystal_managed2003_chs.msm
(For all other files required for running the report, the database accesses the MSM to process the distribution. These include database, export, and chart drivers .)
* Crystal_database_access2003.msm
Crystal_database_access2003_chs.msm
(Keycode MSM processes the installation of the crystal decisions key number. Note that the merging module is added; otherwise, the "mergemouduleproperties" attribute is not available)
* Crystal_regwiz2003.msm
(If the report file uses the dataset DataSet object of ADO. net, the vc_user_crt71_rtl_x86 _ ---. MSM and vc_user_stl71_rtl_x86 _ ---. MSM modules must also be included in the installation project. In addition, the "module retargetable folder" item of the File Installation attribute of the two modules must be changed to the system directory)
Vc_user_crt71_rtl_x86 _ ---. MSM
Vc_user_stl71_rtl_x86 _ ---. MSM
(Many people often encounter query errors. You may add this)
5. open solution --> right-click the attribute of crystal_regwiz2003.msm, and fill in the "license key" in "mergemouduleproperties": aap5gks1_gde100ds (this is the registration code used to generate the crystal report !)
7). added the uninstall function during packaging:
Method 1:
1. Upload the msiexec.exe file in the package object (generally found in C: windowssystem32)
2. Upload the program file in the file system, press "right" on "msiexec.exe", select "Uninstall" to create a shortcut, and rename the shortcut to "Uninstall ".
3. Change the arguments of this shortcut to "/X {product ID}", and the value of product ID is the productcode validation value of the packaging destination.
Method 2: (recommended)
1. Complete the installation package, and write down the productcode (select the solution Resource Manager root directory such as setup1, and then view the property tag, not the right-click attribute ).
2.use vs.netto create 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 your own productcode
End if
Myprocess. Close ()
End sub
End Module
3.add the exefiles in the binlog of the console program to the package program file, and create the uninst.exe shortcut in the program group.
Appendix:
Installdb. VB class, to add reference system. configuration. Install. dll:
Imports system. componentmodel
Imports system. configuration. Install
Public class installer1
Inherits system. configuration. Install. Installer
# Region "code generated by the component designer"
Public sub new ()
Mybase. New ()
'The call is required by the component designer.
Initializecomponent ()
'Add any initialization after initializecomponent () is called.
End sub
'Installer override dispose to clear the component list.
Protected overloads overrides sub dispose (byval disposing as Boolean)
If disposing then
If not (components is 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 process is required by the component designer
'You can use the component designer to modify this process.
'Do not use the code editor to modify it.
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
'Installation failed.
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 reverse 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 call automatic batch processing.
Try
'Create a temporary script
Createsql (string. Format ("{0} mydb2000tp. SQL", me. Context. Parameters. Item ("targetdir ")))
'Call osql to execute the 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 the 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

Related Article

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.