ASP源码
PHP源码
.NET源码
JSP源码
在.NET平台下,部署 Web 解决方案是比较方便的。我们可以利用Visual St***o.NET 2003添加一个WEB安装项目,在部署的“文件系统编辑器”中添加项目的主输出和内容文件,非常简易地完成安装程序的制作。
但是,这样制作的安装程序,只是将Web页和ASP.NET程序编译的DLL文件安装到目标机器的IIS目录,对于一般的应用程序是可以的(比如用Access数据库,可以一起打包到安装程序中);如果数据库是SQL SERVER,需要在部署的时候一并安装数据库,安装程序的制作就会复杂一些,需要我们自定义安装程序类。在安装程序类中执行SQL脚本并将连接字符串写入We***onfig。
l 安装数据库
微软MSDN上介绍过在部署应用程序的时候建立数据库。如:
这种方法是创建一个安装程序类,在安装程序类中调用ADO.NET执行SQL 语句(SQL语句放在一个文本文件中)来创建数据库。
但是,这种方法有一个问题,如果用SQL Server2000生成了所有建表、视图、存储过程的一个脚本文件,用ADO.NET来执行这个脚本文件,就会因为脚本中有许多“GO”语句而出现错误。当然,我们可以把“GO”替换成换行符,利用ADO.NET一条条执行SQL 语句。很显然,这样的效率比较低。
最好的办法是调用osql执行脚本。(或者创建一个数据库项目的cmd文件,而cmd文件建立数据库的时候也是调用的osql)。
using System;
using Sy***m.Collections;
using Sy***m.ComponentModel;
using Sy***m.Configuration.Install;
using Sy***m.Data.SqlClient;
using Sy***m.IO;
using Sy***m.Reflection;
using Sy***m.Diagnostics;
using Sy***m.Xml;
namespace DBCustomAction
{
///
/// DBCustomAction 的摘要说明。
///
[RunInstaller(true)]
public class DBCustomAction : Sy***m.Configuration.Install.Installer
{
///
///@author:overred
///
private Sy***m.ComponentModel.Container components = null;
public DBCustomAction()
{
// 该调用是设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
co***nents.Dispose();
}
}
ba***Dispose( disposing );
}
#region 组件设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
components = new Sy***m.ComponentModel.Container();
}
#endregion
#region custom setup
private void ExecuteSql(string connString,string DatabaseName,string sql)
{
SqlConnection conn=new SqlConnection(connString);
SqlCommand cmd=new SqlCommand(sql,conn);
co***Open();
cm***onnection.ChangeDatabase(DatabaseName);
try
{
cm***xecuteNonQuery();
}
catch(Exception e)
{
StreamWriter w=new StreamWriter(@"e:\log.txt",true);
w.***teLine("===in ExecuteSql======");
w.***teLine(e.***tring());
w.Close();
}
finally
{
co***Close();
}
}
public override void Install(IDictionary stateSaver)
{
createDB();
updateConfig();
}
private void createDB()
{
try
{
string co***tring=st***g.Format("server={0};user id={1};password={2}",th***Context.Parameters["server"],th***Context.Parameters["user"],th***Context.Parameters["pwd"]);
//根据输入的数据库名称建立数据库
ExecuteSql(connString,"master","create database "+th***Context.Parameters["dbname"]);
//调用osql执行脚本
string cm***tring.Format(" -S{0} -U{1} -P{2} -d{3} -i{4}db.sql",th***Context.Parameters["server"],th***Context.Parameters["user"],th***Context.Parameters["pwd"],th***Context.Parameters["dbname"],th***Context.Parameters["targetdir"]);
Sy***m.Diagnostics.Process sqlProcess=new Process();
sq***ocess.StartInfo.FileName="osql.exe";
sq***ocess.StartInfo.Arguments=cmd;
sq***ocess.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
sq***ocess.Start();
sq***ocess.WaitForExit();//等待执行
sq***ocess.Close();
//删除脚本文件
Sy***m.IO.FileInfo sqlFileInfo=new FileInfo(st***g.Format("{0}db.sql",th***Context.Parameters["targetdir"]));
if(sq***leInfo.Exists)
sq***leInfo.Delete();
}
catch(Exception e)
{
StreamWriter w=new StreamWriter(@"e:log.txt",true);
w.***teLine("===in Install======");
w.***teLine(e.***tring());
w.Close();
}
}
private void updateConfig()
{
try
{
//将连接字符串写入We***onfig
Sy***m.IO.FileInfo fileInfo=new FileInfo(st***g.Format("{0}we***onfig",th***Context.Parameters["targetdir"]));
if(!fi***nfo.Exists)
throw new InstallException("can't find the we***onfig");
XmlDocument doc=new XmlDocument();
doc.Load(fi***nfo.FullName);
bool foundIt=false;
string co***tring=st***g.Format("server={0};database={1};user id={2};password={3}",th***Context.Parameters["server"],th***Context.Parameters["dbname"],th***Context.Parameters["user"],th***Context.Parameters["pwd"]);
string en***SecurityHelper.EncryptDBConnectionString(connString);
XmlNode no***c.SelectSingleNode("//appSettings/add[@key='connString']");
if(no!=null)
{
no***tributes.GetNamedItem("value").Value=enCS;
foundIt=true;
}
if(!foundIt)
throw new InstallException("can't find the connString setting ");
doc.Save(fi***nfo.FullName);
}
catch(Exception e)
{
StreamWriter w=new StreamWriter(@"e:log.txt",true);
w.***teLine("===in updata connstring=tjtj=====");
w.***teLine(e.***tring());
w.***teLine(e.***ckTrace);
w.Close();
}
}
#endregion
}
}