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

如何使用JenkinsPipeline构建PHP程序的持续打包部署流程?Jenkins是一款非常流行的持续集成和部署工具,它提供了丰富的插件和功能,使得构建和部署过程变得简单而高效。而JenkinsPipeline是Jenkins最新推出的插件,它允许我们使用一种完整的、可扩展的DSL(DomainSpecificLanguage)来定义持续集成和部

如何在Linux服务器上部署可信赖的Web接口?简介:在如今信息爆炸的时代,Web应用已经成为了人们获取信息和进行交流的主要途径之一。为了确保用户的隐私安全和信息的可靠性,我们需要在Linux服务器上部署一个可信赖的Web接口。本文将介绍如何在Linux环境下进行Web接口的部署,并提供相关的代码示例。一、安装和配置Linux服务器首先,我们需要准备一个Li

Laravel是一个极受欢迎的PHP开发框架,它以其简洁、优雅和高效的特性得到了众多开发者的青睐。随着Laravel的不断发展,LaravelEnvoyer作为一种部署工具,可帮助开发者更容易地将应用程序部署在服务器上。本文将向您介绍如何使用LaravelEnvoyer快速、轻松地部署应用程序。LaravelEnvoyer是什么?LaravelEnv

如何在Linux上部署Web应用程序随着互联网的发展,Web应用程序的开发和部署变得越来越流行。而Linux是Web服务器的首选操作系统。本文将介绍如何在Linux上部署Web应用程序,并附上一些常见的代码示例。安装必要的软件在开始之前,我们需要安装一些必要的软件,包括Web服务器(如Apache、Nginx等)、PHP解释器(如果你的应用程序使用了PHP)

随着现代互联网应用程序的不断发展和复杂性的增加,网络爬虫已经成为数据获取和分析的重要工具。而Scrapy作为Python最流行的爬虫框架之一,拥有强大的功能和易于使用的API接口,可以帮助开发人员快速地抓取和处理Web页面数据。但是,当面对大规模抓取任务时,单个Scrapy爬虫实例很容易受到硬件资源限制,因此通常需要将Scrapy容器化并部署到Docker容

使用Webman实现网站的持续集成和部署随着互联网的迅猛发展,网站开发和维护的工作也变得越来越复杂。为了提高开发效率和保证网站的质量,采用持续集成和部署的方式成为了一个重要的选择。在这篇文章中,我将介绍如何使用Webman工具来实现网站的持续集成和部署,并附上一些代码示例。一、什么是WebmanWebman是一个基于Java的开源持续集成和部署工具,它提供了

如何在Linux上部署微服务架构微服务架构已经成为现代软件开发中的热门话题。它将一个大型应用程序拆分成多个独立的小型服务,每个服务都可以独立开发、测试、部署和扩展。这种架构能够改善系统的可维护性、可扩展性和可测试性。在本篇文章中,我们将讨论如何在Linux操作系统上部署微服务架构。首先,我们需要为每个微服务创建一个独立的容器。容器是一种虚拟化技术,它可以提供

随着互联网技术的快速发展,微服务架构也越来越被广泛应用。使用微服务架构可以有效避免单体应用的复杂度和代码耦合,提高应用的可扩展性和可维护性。然而,与单体应用不同,在微服务架构中,服务数量庞大,每个服务都需要进行自动化测试和部署,以确保服务的质量和可靠性。本文将针对微服务架构中如何处理服务的自动化测试和部署进行探讨。一、微服务架构中的自动化测试自动化测试是保证


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
