这次的项目整个都比较正规,所以即便是web的,也需要制作完整的安装文件,首先我想到的是InstallShield或者installanywhere。
先是installanywhere,尝试自己做了一下,好像功能有点弱,所以没有继续研究,转而去下了个InstallShield 10,集成到VS.NET中,虽然可以按照向导一步一步走,但是最后得到的结果还是让我不满意。所以决定还是用VS.NET。
网上放狗一搜,发现很多可用资源,现在都列出几个——
1、部署ASP.NET的三大技术;
2、部署.net平台的程序;
3、.NET平台下WEB应用程序的部署(安装数据库和自动配置) ;
第三个也就是很重要的,一般来说web项目都需要建立数据库,所以我就主要参考了这个文档,对其中给出的VB.NET代码改造为C#(这个让我很头疼了一会儿,这里要感谢GoodIdea、大鱼、小峰的帮助),在此我给出我调试好的C#代码(其中加入了如果创建数据库时存在同名的数据库会自动drop掉)——
1 using System;
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Configuration.Install;
5 using System.IO;
6 using System.Reflection;
7 using System.Diagnostics;
8 //using System.Data;
9 //using System.Data.SqlClient;
10
11 namespace DBCustomAction
12 {
13 /// <summary>
14 /// DBCustomAction 的摘要说明。
15 /// </summary>
16 [RunInstaller(true)]
17 public class DBCustomAction : System.Configuration.Install.Installer
18 {
19 /// <summary>
20 /// 必需的设计器变量。
21 /// </summary>
22 private System.ComponentModel.Container components = null;
23
24 public DBCustomAction()
25 {
26 // 该调用是设计器所必需的。
27 InitializeComponent();
28
29 // TODO: 在 InitializeComponent 调用后添加任何初始化
30 }
31
32 /// <summary>
33 /// 清理所有正在使用的资源。
34 /// </summary>
35 protected override void Dispose( bool disposing )
36 {
37 if( disposing )
38 {
39 if(components != null)
40 {
41 components.Dispose();
42 }
43 }
44 base.Dispose( disposing );
45 }
46
47
48 #region 组件设计器生成的代码
49 /// <summary>
50 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
51 /// 此方法的内容。
52 /// </summary>
53 private void InitializeComponent()
54 {
55 components = new System.ComponentModel.Container();
56 }
57 #endregion
58
59 //执行SQL 语句
60 private void ExecuteSql(string conn,string DatabaseName,string sql)
61 {
62 System.Data.SqlClient.SqlConnection mySqlConnection = new System.Data.SqlClient.SqlConnection(conn);
63 System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(sql,mySqlConnection);
64 Command.Connection.Open();
65 Command.Connection.ChangeDatabase(DatabaseName);
66 try
67 {
68 Command.ExecuteNonQuery();
69 }
70 finally
71 {
72 Command.Connection.Close();
73 }
74 }
75
76 public override void Install(System.Collections.IDictionary stateSaver)
77 {
78 //------------------------建立数据库-------------------------------------------------
79 try
80 {
81 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"]);
82 //根据输入的数据库名称建立数据库
83 //如果输入库存在先删除
84 string checkDatabase = "if exists (select name from master..sysdatabases where name='" + this.Context.Parameters["dbname"] + "') drop database " + this.Context.Parameters["dbname"];
85 ExecuteSql(connStr,"master",checkDatabase);
86 //创建新的数据库
87 ExecuteSql(connStr,"master","CREATE DATABASE " + this.Context.Parameters["dbname"]);
88 //调用osql执行脚本
89 System.Diagnostics.Process sqlProcess = new Process();
90 sqlProcess.StartInfo.FileName = "osql.exe";
91 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"]);
92 sqlProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
93 sqlProcess.Start();
94 sqlProcess.WaitForExit(); //等待执行
95 sqlProcess.Close();
96 //删除脚本文件
97 System.IO.FileInfo sqlFileInfo = new FileInfo(string.Format("{0}db.sql", this.Context.Parameters["targetdir"]));
98 if (sqlFileInfo.Exists)
99 {
100 sqlFileInfo.Delete();
101 }
102
103 }
104 catch(Exception ex)
105 {
106 throw ex;
107 }
108
109 // ---------------------将连接字符串写入Web.config-----------------------------------
110 try
111 {
112 System.IO.FileInfo FileInfo = new FileInfo(this.Context.Parameters["targetdir"] + "\\web.config");
113 if (!FileInfo.Exists)
114 {
115 throw new InstallException("没有找到配置文件");
116 }
117
118 //实例化XML文档
119 System.Xml.XmlDocument XmlDocument = new System.Xml.XmlDocument();
120 XmlDocument.Load(FileInfo.FullName);
121 //查找到appSettings中的节点
122 bool FoundIt = false;
123 //XmlNode root = XmlDocument.FirstChild;
124 //System.Xml.XmlNode appNode = XmlDocument.SelectSingleNode("configuration").SelectSingleNode("appSettings");
125 foreach ( System.Xml.XmlNode n in XmlDocument.SelectSingleNode("configuration").SelectSingleNode("appSettings"))
126 {
127 if ( n.Name == "add" )
128 {
129 if ( n.Attributes["key"].Value == "dsn" )
130 {
131 n.Attributes["value"].Value = string.Format("Persist Security Info=False;Data Source={0};Initial Catalog={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1",this.Context.Parameters["server"], this.Context.Parameters["dbname"], this.Context.Parameters["user"], this.Context.Parameters["pwd"]);
132 FoundIt = true;
133 }
134 }
135 }
136 if (!FoundIt)
137 {
138 throw new InstallException("web.Config 文件没有包含dsn连接字符串设置");
139 }
140 XmlDocument.Save(FileInfo.FullName);
141 }
142 catch(Exception ex)
143 {
144 throw ex;
145 }
146 }
147 }
148 }
149
之后,只要去下载一个Microsoft Visual Studio .NET 2003 引导程序插件,生成安装项目就可以了。整个安装程序实现的功能如下——
1、安装时侦测客户的服务器是否安装了.NET框架,如果没有安装,会自动提示安装;
2、安装程序自动侦测客户服的务器的MDAC版本是否是达到需要,如果没有,自动安装;
3、安装程序自动按照用户要求创建数据库,并建立站点虚拟目录;
其它制作安装程序的步骤,上面我给出的文档都已经说得很详细了,为一的不足,就是让用户填入数据库用户的密码时,是直接显示的,而不是隐藏的,我也正在找如何解决这个问题。
浙公网安备 33010602011771号