Detailed Method for web program packaging and automatic database creation

Source: Internet
Author: User
Tags throw exception

Use vs2005 to create the installation package.
1. Select the "other project types"-> "setup and deployment" node in the tree on the left of the "new project" dialog box, and select "Web Setup project" on the right ".
 
2. In Solution Explorer, right-click solution and choose add> existing web site to add the folder of the compiled web site to solution.
 
If you add a website compiled with aspnet_compiler, the following prompt box may appear. Click "yes.
 

3. Add a new "class library" named "createdb" to create a database.
 

Delete the default generated "class1.cs". Right-click the project and choose "add"> "new item". In the displayed dialog box, select "Installer class" and click "OK.
 
Add the following code to the class:
Private void executesql (string connectionstring, string databasename, string SQL)
{
Sqlconnection = new sqlconnection (connectionstring );
Sqlcommand = new sqlcommand (SQL, sqlconnection );
Try
{
Sqlcommand. Connection. open ();
Sqlcommand. Connection. changedatabase (databasename );
Sqlcommand. executenonquery ();
}
Catch (exception)
{
Throw exception;
}
Finally
{
Sqlcommand. Connection. Close ();
}
}

Public override void install (system. Collections. idictionary statesaver)
{
String Server = This. Context. Parameters ["server"];
String database = This. Context. Parameters ["dbname"];
String user = This. Context. Parameters ["user"];
String Password = This. Context. Parameters ["PWD"];
String targetdir = This. Context. Parameters ["targetdir"];

Try
{
String connectionstring = string. Format ("Data Source = {0}; user id = {1}; Password = {2}; persist Security info = false; packet size = 4096 ",
Server, user, password );

// Create DB
Executesql (connectionstring, "Master", "create database" + database );

// Set User
String setuserstring = "sp_addlogin 'printeryerp ', 'printeryerp', 'printeryerp '";
String setaccessstring = "sp_grantdbaccess 'printeryerp '";
String setrole = "sp_addrolemember 'db _ owner', 'printeryerp '";

// Create new user login
Try
{
Executesql (connectionstring, "Master", setuserstring );
}
Catch {}

// Set default database
Try
{
Executesql (connectionstring, "printeryerp", setaccessstring );
}
Catch {}

// Set read role
Try
{
Executesql (connectionstring, "printeryerp", setrole );
}
Catch {}

// Create table, store produce ......
Process osqlprocess = new process ();
Osqlprocess. startinfo. filename = targetdir + "osql.exe ";
Osqlprocess. startinfo. arguments = string. format ("-U {0}-P {1}-s {2}-d {3}-I {4} createdb. SQL ",
User, password, server, database, targetdir );
Osqlprocess. startinfo. windowstyle = processwindowstyle. hidden;
Osqlprocess. Start ();
Osqlprocess. waitforexit ();
Osqlprocess. Close ();

// Add data
Osqlprocess. startinfo. arguments = string. format ("-U {0}-P {1}-s {2}-d {3}-I {4} insertdata. SQL ",
User, password, server, database, targetdir );
Osqlprocess. startinfo. windowstyle = processwindowstyle. hidden;
Osqlprocess. Start ();
Osqlprocess. waitforexit ();
Osqlprocess. Close ();
}
Catch (exception)
{
Throw exception;
}

Try
{
String configfile = targetdir + "/Web. config ";
If (! File. exists (configfile ))
{
Throw new installexception ("the configuration file is not found. ");
}

Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (configfile );

GC. Collect ();
File. Delete (configfile );
GC. Collect ();

Foreach (xmlnode in xmldoc ["configuration"] ["connectionstrings"]. childnodes)
{
If (xmlnode. Name = "add ")
{
If (xmlnode. attributes ["name"]. value = "dbconnection ")
{
Xmlnode. attributes ["connectionstring"]. value = string. format ("Data Source = {0}; initial catalog = {1}; persist Security info = true; user id = printeryerp; Password = printeryerp ",
Server, database );
}
If (xmlnode. attributes ["name"]. value = "usermanageservicesconnection ")
{
Xmlnode. attributes ["connectionstring"]. value = string. format ("Data Source = {0}; initial catalog = {1}; persist Security info = true; user id = printeryerp; Password = printeryerp ",
Server, database );
}
}
}
Xmldoc. Save (configfile );
}
Catch (exception)
{
Throw exception;
}
}
4. Right-click the "Web Setup project" project, choose "add"> "project output", select "createdb", select "primary output", and click "OK. Repeat the preceding action to select the added web site, select "content Files", and click OK.

 

5. Right-click the "Web Setup project" project and choose "add"> "file" to add the createdb. SQL script for creating database tables, stored procedures, and views. Repeated. The script insertdata. SQL for adding basic data to the data table is added. Replay and add the program osql.exe.
6. Right-click the "Web Setup project" project and choose "add"> "Merge Module". In the displayed dialog box, select "vc_user_crt71_rtl_x86 _ ---. MSM" and click OK. The reason for testing the vcpus on a clean machine is that the osql.exe program needs this library.
7. Right-click the "Web Setup project" project and select "properties". In the displayed dialog box, you can set the properties of some installation programs. Click prerequisites. In the displayed dialog box, select. NET Framework 2.0 and Windows Installer 3.1, and select download prerequisites from the same location as my application ". In this way, these components and applications can be packaged together and automatically detected and installed during installation. If the computer to be deployed has not been installed with the latest patch, there is no "Windows Installer 3.1". Without this component, ". NET Framework 2.0" cannot be installed.

8. right-click the "Web Setup project" project, choose "View"> "Custom Actions", and right-click the node "Install" on the tree chart that appears, select "add M actions ". In the displayed dialog box, select "Web Application folders" in "look in", select "primary output from createdb (active)" below, and click "OK.
 

9. right-click the "Web Setup project" project, choose "View"> "User Interface", and right-click the child node "start" of the "Install" tree chart node, select "add dialog". In the displayed dialog box, select "textboxes ()".
 
Right-click the newly added node "textboxes (a)", select "properites window", and set the edit * property to "mmtexta1" and "customtexta2" in sequence ", "mmtexta3" and "customtexta4 ".

10. right-click the created "primary output from createdb (active)" node and select "Properties window ", set the "customactiondata" attribute to "/dbname = [customtexta1]/Server = [customtexta2]/user = [customtexta3]/Pwd = [customtexta4]/targetdir =" [targetdir]/" ".

Next, compile the entire solution and generate two folders and two files in the output directory.
The two folders contain the installation packages of. NET Framework 2.0 and Windows installer3.1 respectively. The two files are classified as (.msi.msiand setup.exe. If you want to install it, execute setup.exe.

 

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.