Use Visual Studio to generate the installer and set the connection string and other custom operations

Source: Internet
Author: User
Tags connectionstrings

Many people may have this problem. Now we have a complete solution.

1. add an installation project (of course, your other projects should be OK. now we have prepared a winui project. remember to create an app. config file, and the configuration section of the connection string must be saved in. otherwise, an error will occur. because I did not handle the error .)

Currently, the connectionstring value of the app. config file is null and must be entered during installation.

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Connectionstrings>
<Add name = "connectionstring" connectionstring = "" providername = "system. Data. sqlclient"/>
</Connectionstrings>
</Configuration>

2. Add project output to the installation project, such as the UI Layer, business logic layer, data operation layer, entity layer, and public layer.

3. Select the user desktop on the left and right-click on the right to create a new shortcut.

4. Generate the installation project to generate the installationProgramIt's not over yet.

5. During the installation process, you need to enter the database connection information or create a database. You need to customize the installation process in the user interface view.

6. Add a dialog box for users to enter information

7.
Modify the attributes of a text box. You can have up to four text boxes. Define the title of the window (bannertext), the description of the window (bodytext), and the label values of each text box.
(Edit_label), corresponding attribute name (edit_property), default value (edit_value), whether to be visible (edit_visible), and other attributes
.

8. Create a new class library project to implement the customized installation function.

9. Create a function implementation class

10.CodeSee the following (note the code comment ):

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. configuration. Install;
Using system. configuration;
Using system. xml;
Using system. IO;

Namespace setsetup
{
/// <Summary>
/// Inherit the installation class
/// Www.szitr.com
/// </Summary>
[Runinstaller (true)]
Public partial class setsqlconstr: Installer
{
Public setsqlconstr ()
{
Initializecomponent ();
}
/// <Summary>
/// Override the installation method of the base class
/// </Summary>
/// <Param name = "statesaver"> </param>
Public override void install (system. Collections. idictionary statesaver)
{
Base. Install (statesaver );
// Obtain the user input parameters
// The parameter is from the custom operation parameter in the next step.
// Custom operation parameter attributes customactiondata: /sqlserverip = [sqlserver_name]/database = [database_name]/username = [username]/Password = [Password]/targetdir = "[targetdir] \"
// The edit_property value of each text box is enclosed in brackets. The last targetdir is the installation path. Note that there is an anti-diagonal bar next to it.
String sqlserverip = This. Context. Parameters ["sqlserverip"];
String database = This. Context. Parameters ["Database"];
String username = This. Context. Parameters ["username"];
String Password = This. Context. Parameters ["password"];
String targetdir = This. Context. Parameters ["targetdir"];
// Write the code you want to execute
// Combine the connection string
String constring = string. format ("Data Source = {0}; initial catalog = {1}; user id = {2}; Password = {3}; persist Security info = true", sqlserverip, database, username, password );
// Change the value of the new connection string. Change winui.exe. config to the name of your configuration file.
Updateconconfig ("connectionstring", constring, targetdir + "winui.exe. config ");
}

/// <Summary>
/// Modify the value of the config map connection string
/// </Summary>
/// <Param name = "conname"> connection string name </param>
/// <Param name = "constring"> connection string </param>
/// <Param name = "configfilepath"> set the file path and name </param>
Public static void updateconconfig (string conname, string constring, string configfilepath)
{
Xmldocument xmldoc = new xmldocument ();
// Read the configuration file
Xmldoc. Load (configfilepath );
// The node that gets the connection string
Xmlnode = xmldoc. selectsinglenode ("configuration/connectionstrings/Add [@ name = '" + conname + "']");
// Modify the connection string
Xmlnode. attributes ["connectionstring"]. innertext = constring;
// Save
Xmldoc. Save (configfilepath );
}

Public override void uninstall (system. Collections. idictionary savedstate)
{
Base. Uninstall (savedstate );
}
Public override void commit (system. Collections. idictionary savedstate)
{
Base. Commit (savedstate );
}
Public override void rollback (system. Collections. idictionary savedstate)
{
Base. rollback (savedstate );
}
}
}

11. Go back to the installation project. Open the custom operation view.

12. add custom operations during installation

13. Add the master output of the project created in step 1

14. Modify the customactiondata attribute of this custom operation

Customactiondata:/sqlserverip = [sqlserver_name]
/Database = [database_name]/username = [username]/Password = [Password]
/Targetdir = "[targetdir] \"

15. generate a new one. Run the installer again. It's OK!

Finally, let's take a look at our profile content.

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Connectionstrings>
<Add name = "connectionstring" connectionstring = "Data Source = dbserver; initial catalog = szitrdb; user id = szitr.com; Password = szitr.com; persist Security info = true" providername = "system. data. sqlclient "/>
</Connectionstrings>
</Configuration>

Successful!

I hope to help you.

code download: http://szitr.com/bbs/thread-131-1-1.html

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.