您的位置:首页 > 其它

用Visual Studio 2005 制作Web安装程序

2006-11-20 16:18 477 查看
制作安装程序不仅仅是将网页做成,还需要将数据库随安装程序一起进行安装。
1.我们添加一个安装程序类。选择“文件”--“添加”--“新建项目”,选择Visual C# 中的“类库”。
2.在上步操作的基础上,进行“添加新项”,选择“安装程序类”,并命名的名字。
3.填充以下代码:


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Configuration.Install;


using System.IO;


using System.Data;


using System.Data.SqlClient;




namespace SetupClassLibrary




...{


partial class MyInstaller




...{




/**//// <summary>


/// 必需的设计器变量。


/// </summary>


private System.ComponentModel.IContainer components = null;






/**//// <summary>


/// 清理所有正在使用的资源。


/// </summary>


/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>


protected override void Dispose(bool disposing)




...{


if(disposing && (components != null))




...{


components.Dispose();


}


base.Dispose(disposing);


}


public override void Install(System.Collections.IDictionary stateSaver)




...{


base.Install(stateSaver);


//获取安装路径参数信息


string path = this.Context.Parameters["path"];


if(path == "")




...{


throw (new InstallException("目标路径错误"));


}


System.IO.StreamWriter Log = new System.IO.StreamWriter(path + "logDogManage.txt");


Log.AutoFlush = true;


//获取用户名


//获取密码


//获取数据库服务器名称


string id = this.Context.Parameters["id"];


string pwd = this.Context.Parameters["pwd"];


string dbname = this.Context.Parameters["dbname"];


if(id == "" || dbname == "")




...{


throw (new InstallException("必须输入用户名和密码"));


}


Log.Write(" 获取用户名和密码成功");


string cfgPath = path + "web.config";


Log.Write(" --------以下是输出an装过的中参数------");


Log.Write(" database server=" + this.Context.Parameters["server"].ToString());


Log.Write(" user name=" + this.Context.Parameters["id"].ToString());


Log.Write(" user password=" + this.Context.Parameters["pwd"].ToString());


Log.Write(" database name=" + this.Context.Parameters["dbname"].ToString());


Log.Write(" path=" + this.Context.Parameters["path"].ToString());


Log.Write(" --------------------------------------");


//======================================================


//进行测试


//======================================================


try




...{


//定义连接字符串


string ConnStr="";


//默认连接字符串


ConnStr = string.Format("Data Source=localhost;Initial Catalog=master;Persist Security Info=True;User ID={0};Password={1}",


id, pwd);




//输出连接数据库字符串


Log.Write(" 连接数据库字符串为:" + ConnStr);


//根据输入的数据库名称建立数据库


Log.Write(" 根据输入的数据库名称建立数据库开始...");


this.ExecuteSql(ConnStr, "master", "Create DataBase " + this.Context.Parameters["dbname"].ToString());


Log.Write(" 根据输入的数据库名称建立数据库完毕!");


//新建数据库的连接字符串


ConnStr = System.String.Format("Data Source=localhost;Initial Catalog={0};Persist Security Info=True;User ID={1};Password={2}",


dbname, id, pwd);


Log.Write(" 连接数据库字符串为:" + ConnStr);


//调用osql执行脚本文件


Log.Write(" 调用osql执行脚本开始...");


Log.Write(" 引用osql系统进程开始...");


System.Diagnostics.Process SqlProcess = new System.Diagnostics.Process();


SqlProcess.StartInfo.FileName = "osql.exe";


SqlProcess.StartInfo.Arguments = System.String.Format(


" -U {0} -P {1} -d {2} -i {3}sql.sql",


this.Context.Parameters["id"], this.Context.Parameters["pwd"],


this.Context.Parameters["dbname"], this.Context.Parameters["path"]);


SqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;


SqlProcess.Start();


Log.Write(" 引用osql开始执行...");


SqlProcess.WaitForExit();//等待执行


Log.Write(" 引用osql等待执行...");


SqlProcess.Close();


SqlProcess.Dispose();


Log.Write(" 引用osql执行完毕...");


Log.Write(" 调用osql执行脚本完毕!");




//'删除脚本文件


Log.Write(" 删除脚本文件开始...");


System.IO.FileInfo SqlFileInfo = new FileInfo(System.String.Format("{0}sql.sql", this.Context.Parameters["path"]));


if(SqlFileInfo.Exists)




...{


SqlFileInfo.Delete();


}


Log.Write(" 删除脚本文件完毕!");


}


catch(Exception e)




...{


Log.Write(e.Message);


throw new InstallException(e.Message);


}




//修改配件文件:Web.config


try




...{


Log.Write(" 查找配置文件...");


System.IO.FileInfo fileinfo = new FileInfo(this.Context.Parameters["path"] + "web.config");




Log.Write(" 配置文件:" + fileinfo.FullName);


if(fileinfo.Exists == false)


throw new InstallException("没有找到配置文件");




//实例化xml文档


Log.Write(" 实例化配置文件...");


System.Xml.XmlDocument xmldocument = new System.Xml.XmlDocument();


xmldocument.Load(fileinfo.FullName);




//'查找到appsettings中的节点


Log.Write(" 查找数据库连接节点...");


System.Xml.XmlNodeList mynodes = xmldocument.DocumentElement.SelectNodes("//configuration/connectionStrings/add");




bool FoundIt = false;




//修改数据库连接配置;


Log.Write(" 修改数据库连接节点...");


foreach(System.Xml.XmlNode n1 in mynodes)




...{


Log.Write(" 修改数据库连接节点..." + n1.LocalName + n1.Name);


if(n1.Attributes["name"].Value == "DogsManagerConnectionString")




...{


Log.Write(" 查找到数据库的一个连接节点...");


n1.Attributes["connectionString"].Value = System.String.Format("data source={0};initial catalog={1};user id={2};password={3}",


this.Context.Parameters["server"].ToString(), this.Context.Parameters["dbname"].ToString(), this.Context.Parameters["id"].ToString(), this.Context.Parameters["pwd"].ToString());


n1.Attributes["providerName"].Value = "System.Data.SqlClient";


FoundIt = true;


break;


}


}




if(FoundIt == false)


throw new InstallException("web.Config文件没有包含connString连接字符串设置");


else




...{ Log.Write(" 修改数据库连接成功!"); }




//查找水晶报表的数据库连接配置;


Log.Write(" 查找报表连接节点...");


FoundIt = false;


//定义三个bool类型的变量,当三个bool均为真时,则表明修改报表 连接配置成功!


bool bol1 = false, bol2 = false, bol3 = false;


System.Xml.XmlNodeList nodes = xmldocument.DocumentElement.SelectNodes("//configuration/appSettings/add");




//修改报表连接配置;


Log.Write(" 修改报表连接节点...");


foreach(System.Xml.XmlNode n in nodes)




...{


if(n.Attributes["key"].Value == "DBUserID")




...{


n.Attributes["value"].Value = this.Context.Parameters["id"].ToString();


Log.Write(" 修改DBUserID的值为:" + this.Context.Parameters["id"].ToString());


bol1 = true;


}


if(n.Attributes["key"].Value == "DBPassWord")




...{


n.Attributes["value"].Value = this.Context.Parameters["pwd"].ToString();


Log.Write(" 修改DBPassWord的值为:" + this.Context.Parameters["pwd"].ToString());


bol2 = true;


}


if(n.Attributes["key"].Value == "DBServerName")




...{


n.Attributes["value"].Value = this.Context.Parameters["server"].ToString();


Log.Write(" 修改DBServerName的值为:" + this.Context.Parameters["server"].ToString());


bol3 = true;


}




}


FoundIt = bol1 && bol2 && bol3;


if(FoundIt == false)


throw new InstallException("web.Config文件没有包含服表连接字符串设置");


Log.Write(" 修改报表连接成功!");




xmldocument.Save(fileinfo.FullName);


}


catch(Exception e)




...{


//输出异常信息


Log.Write(" " + e.Message);


throw new InstallException(e.Message);


}


finally




...{


Log.Dispose();


this.Dispose();


}


}




/**/////


////执行sql 语句


////用于创建数据库


////


private void ExecuteSql(string conn, string DataBaseName, string sql)




...{


SqlConnection mySqlConnection = new SqlConnection(conn);


SqlCommand Command = new SqlCommand(sql, mySqlConnection);


Command.Connection.Open();


Command.Connection.ChangeDatabase(DataBaseName);




try




...{


Command.ExecuteNonQuery();


}


catch (Exception e)




...{


throw (e);


}


finally




...{


Command.Connection.Close();


Command.Dispose();


}


}






组件设计器生成的代码#region 组件设计器生成的代码






/**//// <summary>


/// 设计器支持所需的方法 - 不要


/// 使用代码编辑器修改此方法的内容。


/// </summary>


private void InitializeComponent()




...{


components = new System.ComponentModel.Container();


}




#endregion


}


}

4.“文件”--“添加”--“新建项目”--“安装和部署”--“Web安装项目”,输入名称,单击“确定”。
5.选择“文件系统”,在“web应用程序文件夹”,右键选择“添加--项目输出”。
6.分别将网站的‘内容文件’和上面的安装程序类的“主输出” 和“内容文件”选择进行添加操作。
7.选择“用户界面”,右键选择“添加对话框”选择“文本框A”,单击“确定”
8.对文本框的属性进行如下设置:如图所示



9.右键选择“自定义操作”,在“安装”上右键选择“添加自定义操作”,选择“Web安装程序文件夹”下的哪个上面建立的安装程序类库.
10.在CustomerActionData属性中,输入以下内容:
/id=[USERNAME] /pwd=[PASSWORD] /dbname=[DBNAME] /server=[DBSERVERNAME] /path="[TARGETDIR]/"
11.将创建数据库表的结构,以sql.sql,保存在网站中。
12.进行生成操作。安装程序制作完成
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: