Web Packaging automatic installation of IIS and database _iis

Source: Internet
Author: User
Tags throw exception vc runtime create database
Use VS2005 to make installation packages.
1. Select "Other Project Types"-> the "Setup and Deployment" node in the left tree of the New Project dialog box and select Web Setup Project on the right.

2. Right-click on solution in Solution Explorer, select "Add"-> "Existing Web Site" To add the folder of the compiled Web website to solution.

If you add a site that is compiled using Aspnet_compiler, you may have the following prompt box and click "Yes" to do so.


3. Add a new "Class Library", the name "Createdb", to create the operation of the database.


Delete the default generated "Class1.cs", on this item right click, select "Add"-> "New Item", in the pop-up dialog box select "Installer Class", click OK.

Add the following code to the class:
private void ExecuteSQL (String connectionString, String databaseName, String sql)
{
SqlConnection SqlConnection = new SqlConnection (connectionString);
SqlCommand SqlCommand = new SqlCommand (sql, SqlConnection);
Try
{
SqlCommand.Connection.Open ();
SqlCommand.Connection.ChangeDatabase (DatabaseName);
SqlCommand.ExecuteNonQuery ();
}
catch (Exception 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};p assword={2};p ersist 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 Exception)
{
Throw exception;
}

Try
{
String configfile = TargetDir + "/web.config";
if (! File.exists (ConfigFile))
{
throw new Installexception ("No configuration file found. ");
}

XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (ConfigFile);

Gc. Collect ();
File.delete (ConfigFile);
Gc. Collect ();

foreach (XmlNode 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 Exception)
{
Throw exception;
}
}
4. Right-click on the Web Setup Project project, select "Add"-> "Project Output", select Project "Createdb", select "Primary Output", and click OK. Repeat the above action, will select the Web Site just added, select "Content Files", click OK.




5. Right-click on the Web Setup Project project and select "Add"-> "File" to add script Createdb.sql to create database tables, stored procedures, and views. Repeat, the script insertdata.sql to add the underlying data to the datasheet is added. Repeat, add the program Osql.exe.
6. Right-click on the Web Setup Project project, select "Add"-> "Merge Module", select "Vc_user_crt71_rtl_x86_---. MSM" In the pop-up dialog box, and press OK. The VC runtime was added because it was found on a clean machine when the Osql.exe program needed this library.
7. Right-click on the WEB Setup Project project, select Properties, and in the dialog box that pops up, you can set some of the installer's attributes. Click on the button "Prerequisites", select ". NET Framework 2.0" and "Windows Installer 3.1" In the pop-up dialog box, check the Download prerequisites from the same Location as my application ". This allows the components and applications to be packaged together and automatically detected and installed when they are installed. If you need to deploy a computer that is not "Windows Installer 3.1" Without the latest patches, ". NET Framework 2.0" cannot be installed without this component.

8. Right-click on the Web Setup Project project, select "View"-> "Custom Actions", click on the node "Install" in the tree graph, and select "Add Custom Actions". Select "Web application Folders" In the pop-up dialog box, select "Primary output from Createdb (Active)" below and click OK.


9. Right-click on the Web Setup Project project, select "View"-> "User Interface", right-click on the child node "Start" of the Node "Install" that appears, and select "Add Dialog", In the pop-up dialog box, select Textboxes (A).

Right-click on the newly added node "textboxes (A)" and select "Properites window" to set the Edit*property property to "CUSTOMTEXTA1", "CUSTOMTEXTA2", and " CUSTOMTEXTA3 "and" CUSTOMTEXTA4 ".

10. Right-click on the Primary output from Createdb (Active) node that you just established, select Properties window, and set the CustomActionData property to/dbname=[ CUSTOMTEXTA1]/server=[customtexta2]/user=[customtexta3]/pwd=[customtexta4]/targetdir= "[targetdir]/".

Next, the entire solution is compiled, and two folders and two files are generated in the output directory.
The. NET Framework 2.0 and Windows Installer3.1 installation packages are included in two folders. The other two files were (project name). msi and Setup.exe. If you want to install, perform 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.