DOTNET system installer step (including framework installation and database installation configuration) C #

Source: Internet
Author: User

Prepared by the boss a few days agoProgramIf you have never done the installation package again, you can use any tool to create the installation package. Now there are a lot of tools for installation program production (WISE, etc.). I downloaded a try and thought it was too difficult to study. I just wanted to use the installation package created by the. NET installer. However, many problems have been encountered during the production process.

For more information, see Li honggen's "deploying web applications (installing databases and automatic configuration) on the Net Platform )"
I personally use C #, put his VBCodeTranslated.

First, export the database table structure you want to create and use the export Wizard of slqserver to export the table structure. Save it to the DB. SQL file first.

Optional steps: Install the framework Bootstrap plug-in.
After installing this plug-in, you can use Microsoft Visual Studio to create the installation package.. NET 2003 Bootstrap plug-in, you can easily create the installer.. NET Framework 1.1 is redistributed with your application.
The address of the bootstrap plug-in plugininstaller. MSI is:
Http://www.microsoft.com/downloads/details.aspx? Familyid = 627921a0-d9e7-43d6-a293-72f9c370bd19 & displaylang = ZH-CN

Step 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 setup.
3. Click OK to close the dialog box.
4. The project is added to Solution Explorer and opened in the file system editor.

Step 2: Create the installer class
1. Point to "new" on the "file" menu and select "project ".
2. In the "new project" dialog box, select "project" in the "project type" pane, and select "class library" in the "template" pane ". In the "name" box, type dbcustomaction.
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 dbcustomaction.
6. Click OK to close the dialog box. (Code attached)

Step 3: add the project output to the deployment project
1. In the file system Editor, select the application folder. On the "operations" menu, point to "add" and select "project output ".
2. In the "add project output group" dialog box, select your project from the "project" drop-down list.
3. Click OK to close the dialog box.
4. Select "primary output" from the list and click "OK ".

Step 4: create a custom installation dialog box
1. Select the "setup" 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 the 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: gscrm.
11. Select the edit2label attribute and type: Server Name :.
12. Select the edit2property attribute 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: SA User Password :.
18. Select the edit4property attribute and type customtexta4.

Step 5: create a custom operation
1. Select the "setup" 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 "main output from dbcustomaction (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] \".
Attachment/targetdir = "[targetdir] \" is the target path after installation. To obtain the path after installation in the dbcustomaction class, we set this parameter.
In addition, the installation path can also be obtained through reflection:
Dim ASM as system. reflection. Assembly = _
System. reflection. Assembly. getexecutingassembly
Msgbox ("ASM. Location ")

Step 6: Add dB. SQL and other files you need to the project, and then you can package them.

The most important problem was that I had been using it for more than three hours. the installation path selected by the user cannot contain spaces. Why? It turns out that when SQL Server executes the command line in osql, if your installation path contains spaces, that command line cannot be correctly executed. "Osql-u userid-P password-D dbname-I c: \ test \ dB. SQL"

In addition, you can directly modify the installed app. config to complete your configuration.ArticleHave related introduction.

Dbcustomaction. CS

Using system;
Using system. Data. sqlclient;
Using system. collections;
Using system. componentmodel;
Using system. configuration. Install;

Namespace dbcustomaction
{
/// <Summary>
/// Summary of dbcustomaction.
/// </Summary>
[Runinstaller (true)]
Public class dbcustomaction: system. configuration. Install. Installer
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;

Public dbcustomaction ()
{
// This call is required by the designer.
Initializecomponent ();

// Todo: add any initialization after initializecomponent calls
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Code generated by the region component designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
Components = new system. componentmodel. Container ();
}
# Endregion

private void executesql (string connstr, string databasename, string SQL)
{< br> sqlconnection conn = new sqlconnection (connstr );
sqlcommand cmd = new sqlcommand (SQL, Conn);
Conn. open ();
Conn. changedatabase (databasename);
try
{< br> cmd. executenonquery ();
}< br> finally
{< br> Conn. close ();
}< BR >}

Public override void install (system. collections. idictionary statesaver)
{< br> base. install (statesaver);
try
{< br> // ------------------------ create a database ----------------------------
string connstr = string. format ("Data Source = {0}; user id = {1}; Password = {2}; persist Security info = false; packet size = 4096", this. context. parameters ["server"], this. context. parameters ["user"], this. context. parameters ["PWD"]);
executesql (connstr, "Master", "create database" + this. context. parameters ["dbname"]);

system. diagnostics. process sqlprocess = new system. diagnostics. process ();
sqlprocess. startinfo. filename = "osql.exe";
sqlprocess. startinfo. arguments = string. format ("-U {0}-P {1}-d {2}-I {3} dB. SQL ", this. context. parameters ["user"], this. context. parameters ["PWD"], this. context. parameters ["dbname"], this. context. parameters ["targetdir"]);
sqlprocess. startinfo. windowstyle = system. diagnostics. processwindowstyle. hidden;
sqlprocess. start ();
sqlprocess. waitforexit (); // wait for execution
}< br> catch (exception e)
{< br> console. write (E. tostring ();
}< br> finally
{< BR >}

trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 830805

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.