C # Windows Application packaging (Vs2010+sql Server 2008)

Source: Internet
Author: User
Tags visual studio installer

Author: Phantom fungus

Original address: http://blog.csdn.net/qingdujun/article/details/37563661


Development environment: Vs2010+sql Server 2008

Operating system: Win7_32bit flagship edition

Development language: C #

Project name: Student Homestay Management System


Let's start with: How do I package Windows applications?

The first step:

Open VS2010, open the project you want to package, then right-click on "Solution", "Add", "New project" and pop up the interface as shown:

Click on the "Install and deploy" triangle to the left, select "Visual Studio Installer" below, select "Setup Project" and change the name below to "Setup" to click OK.



Step Two:

Click "Setup" generated in the solution to change the protectname in the attribute to "Student Homestay system V1.0" (Your project name)



Step Three:

Right-click "Setup" in the Solution and select "Properties". The Popup property page interface follows the second picture:

Then click on the system prerequisites inside.

Important: Tick the "Download prerequisites from the same location as my application (D)", which means that when you tick, when you build the Setup project, the components that you check in the prerequisites list are available under the path of your installation project. (The system automatically completes, this is good, do not need you to download components)

1), Windows Installer 3.1 (required)

2),. NET Framework 3.5 (optional) Reference last description

3), Crystal Reports Basic for Visual Studio2008 (x86,x64) (optional) in the project, you need to tick this.




Fourth Step:

Right-click on "Application Folder", click "Add", then click on the folder, named "DB" (optional) for storing your database files.

Then right-click on the "DB" folder you just added, "Add", "File" and add your database.




Fifth Step:

Right click on "Application Folder", click "Add", click "File". Add all of the files in your release directory below.



Sixth step:

Right click on "Application Folder", click "Add", select "Project Output ...", note: In the Project column to select your own project (my Project name: studentjisu), then select "Main Output", click OK.




Seventh Step:

Create a desktop shortcut, right-click the main output you just added, then select the first create shortcut, and then you can rename the shortcut (I renamed it: Student Homestay management System)

Finally, click the left mouse button to place the shortcut, and then drag and drop it under the User Desktop folder.




Eighth Step:

Create an uninstall program. Right click on "Application Folder", click "Add", select "File", and then "C:\Windows\System32" under the "Msiexec.exe" file to add, if not found, you can search directly. Of course, you can also create a shortcut for Msiexec.exe named "UnInstall".



After naming the shortcut, copy the Setup property (click the Setup pop-up property in the solution) ProductCode to the arguments of the Uninstall property:

At the same time, add "/X" to the front, note that there is a space behind X.



Nineth Step:

Change the logo of the desktop shortcut. Your own logo is too bad, you can download a picture on the Internet, and then convert to. ico format.

In the "Application Folder" refers to the location of the logo storage, usually exist in that place.



Tenth step:

Attach the database. We will now add a class for writing additional database code.

Right-click on "Solution", click "Add", select "New Project", then create a new C # class library and name it "Installdb".

Finally, delete the words "Class1.cs".




11th Step:

Create a new class for writing the code that the database attaches to the database management system. Right-click the newly created "Installdb", click "Add" and select "New Item".

Then, in the popup screen, select "Installer Class" and name "InstallDB.cs".




12th Step:

Because the attached database requires users to enter some information about the native database, such as: server name, database administrator name and password, and so on. At this point, we can pop up a box waiting for user input during the installation process:

Right click on "Setup", click "View" and select "User Interface". Pop up the second screen below, then right click on "Start", click "Add Dialog", select "text box (A)", and drag it to "welcome" below the third picture below.

Finally, fill in the "text box (A)" attribute according to your own needs, you can refer to the third picture.

Note: The variables defined inside are defined primarily for the additional code below.





13th Step:

Add the primary output of the attached database. Right-click Setup, select View, Custom action.

Then, right-click on "Install", select "Application Folder", select the Installer Class "Installdb", or select "main output" to determine.

Next, copy and paste the following in the Costomactiondata:

/dbname=[dbname]/server=[server]/user=[user]/pwd=[pwd]/targetdir= "[targetdir]\"





14th Step:

Write the additional database code in InstallDB.cs. Click "Stand here to switch to Code view" first.

Then add several namespaces.

Of course, to use the MessageBox () function, you need to add a using System.Windows.Forms; You also need to add a System.Windows.Forms reference (action: Right-click Installdb, select Add Reference, select. NET)


Of course, the last written code is as follows:

Using system;using system.collections;using system.collections.generic;using system.componentmodel;using System.configuration.install;using system.linq;using system.data.sqlclient;using System.Windows.Forms;using System.io;using system.security.accesscontrol;namespace installdb{[Runinstaller (true)] public partial class Install        DB:System.Configuration.Install.Installer {public installdb () {InitializeComponent (); }//CREATE Database private void CreateDatabase (String strSQL, String dataname, String strmdf, String strldf, Stri            ng path) {SqlConnection myconn = new SqlConnection (strSQL);            String str = NULL; try {str = @ "EXEC sp_attach_db @dbname = '" + dataname + "', @filename1 = '" + strmdf + "', @filenam                E2= ' "+ strldf +" ' ";                SqlCommand mycommand = new SqlCommand (str, myconn);                MyConn.Open ();                Mycommand.executenonquery (); MeSsagebox.show ("Database installation succeeded! Click OK to continue ");//need using System.Windows.Forms} catch (Exception e) {MessageBox. Show ("The database installation failed!                "+ e.message +" \ n "+" You can manually append data "); System.Diagnostics.Process.Start (path);//Open installation directory} finally {Myconn.close ()            ; }}//Rights Management private static void Setfullcontrol (string path) {FileInfo info = new Fi            Leinfo (path); FileSecurity fs = info.            GetAccessControl (); Fs.            Addaccessrule (New FileSystemAccessRule ("Everyone", Filesystemrights.fullcontrol, Accesscontroltype.allow)); Info.        Setaccesscontrol (FS);            }//Overloaded install function public override void Install (System.Collections.IDictionary statesaver) { String Server = this. context.parameters["Server"];//server name string uid = this. context.parameters["User"];//sqlserver username string pwd = this. Context.parameters["pwd"];//password string path = this. context.parameters["TARGETDIR"];//installation directory String ch = path. Substring (path.            Length-1, 1); if (ch = = @ "\")//The path is processed to determine if there is ' \ ' Path = path at the end. Substring (0, path. LENGTH-1);//delete String strSQL = "server=" + server + "uid=" + uid + ";p wd=" + pwd + ";d atabase=ma Ster ";//Connection Database string dataname = @" STUBOARDDB ";//Database name string strmdf = path + @" Xsjsglxt.mdf ";//mdf            File path, note that the file name is the same as the database file name you just added!    Setfullcontrol (Strmdf);    Set permissions to everyone string strldf = path + @ "Xsjsglxt_log.ldf";//ldf file path Setfullcontrol (STRLDF); Set permissions to everyone base.            Install (statesaver); This. CreateDatabase (strSQL, Dataname, Strmdf, strldf, path);//Start Database Creation}}}
Perhaps you see more code, in fact, you can reuse these, you just need to change a little bit on the line. Like (something in a black box): you know.


15th Step:

Well, finally, build the installation package.


Reference: Wide Field Blog Park. C # WinForm Packaging (with database installation) [Cp/ol].http://www.cnblogs.com/scottckt/archive/2011/05/14/2046313.html, 2011-05-14/2014-07-08

Hai Fang's CSDN blog. VS2010 c/S mode WinForm packaged deployment. Detailed! [cp/ol].http://blog.csdn.net/xhf55555/article/details/7702212,2012-06-29/2014-07-08

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.