部署安装时写入SQLSERVER和Web.config

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介:
在.NET平台下,部署 Web 解决方案是比较方便的。我们可以利用Visual Studio.NET 2003添加一个WEB安装项目,在部署的“文件系统编辑器”中添加项目的主输出和内容文件,非常简易地完成安装程序的制作。
 
但是,这样制作的安装程序,只是将Web页和ASP.NET程序编译的DLL文件安装到目标机器的IIS目录,对于一般的应用程序是可以的(比如用Access数据库,可以一起打包到安装程序中);如果数据库是SQL SERVER,需要在部署的时候一并安装数据库,安装程序的制作就会复杂一些,需要我们自定义安装程序类。在安装程序类中执行SQL脚本并将连接字符串写入Web.config。
 
l 安装数据库
微软MSDN上介绍过在部署应用程序的时候建立数据库。如:
这种方法是创建一个安装程序类,在安装程序类中调用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
{
/// <summary>
/// DBCustomAction 的摘要说明。
/// </summary>
[RunInstaller(true)]
public class DBCustomAction : System.Configuration.Install.Installer
{
/// <summary>
///@author:overred
/// </summary>
private System.ComponentModel.Container components = null;
public DBCustomAction()
{
// 该调用是设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
/// <summary> 
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
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
}
}
 



















本文转自周金桥51CTO博客,原文链接: http://blog.51cto.com/zhoufoxcn/166971 ,如需转载请自行联系原作者
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
相关文章
|
1月前
|
存储 资源调度 应用服务中间件
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
26 0
|
3月前
|
安全 应用服务中间件 nginx
百度搜索:蓝易云【使用Debian、Docker和Nginx部署Web应用教程】
这些是在Debian上使用Docker和Nginx部署Web应用的基本步骤。根据您的需求和具体环境,可能还需要进行其他配置和调整。请确保在进行任何与网络连接和安全相关的操作之前,详细了解您的网络环境和安全需求,并采取适当的安全措施。
45 0
|
9天前
|
Web App开发 Java 应用服务中间件
【Java Web】在 IDEA 中部署 Tomcat
【Java Web】在 IDEA 中部署 Tomcat
|
1月前
|
安全 搜索推荐 前端开发
如何在本地安装Flask并将其web界面发布到公网上远程访问协同开发
如何在本地安装Flask并将其web界面发布到公网上远程访问协同开发
|
1月前
|
应用服务中间件 Linux nginx
web后端-linux-nginx-1.18操作命令和部署
web后端-linux-nginx-1.18操作命令和部署
|
1月前
|
Java 应用服务中间件
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
129 1
|
1月前
|
安全 应用服务中间件 nginx
|
2月前
|
安全 网络安全 开发者
如何在OpenWRT部署uhttpd搭建服务器实现远程访问本地web站点
如何在OpenWRT部署uhttpd搭建服务器实现远程访问本地web站点
114 0
|
2月前
|
SQL 存储 数据安全/隐私保护
|
2月前
|
SQL 数据管理 数据库
【安装指南】从零开始:SQL Server 2019与SSMS完美安装攻略!
【安装指南】从零开始:SQL Server 2019与SSMS完美安装攻略!
50 0