Asp.net网站的自动部署-sqlserver数据库的自动部署
Asp.net网站的自动部署
2005-09-28
这段时间要解决的问题是asp.net网站的自动部署,主要功能是在安装的时候实现数据库的部署和基本数据的添加,asp.net网站的部署(多个虚拟目录)和xml配置文件的修改。
在baidu里搜了一下asp.net自动安装部署的文章差不多就两篇,都是关于数据库的自动部署方面的,有兴趣的可以参考:
1、李洪根:.NET平台下WEB应用程序的部署(安装数据库和自动配置)
2、MSDN:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxwlkwalkthroughusingcustomactiontocreatedatabaseduringinstallation.asp
今天先讨论数据库的部署的问题,暂时只考虑sqlserver2000数据库,并且暂时不考虑数据库数据的添加,只是在安装的时候在客户的数据库服务器上把数据库建起来。
内容:
(1)数据库脚本的准备
(2)数据库自动部署类(DBInstaller)
数据库脚本的准备
在mssql中数据库脚本的准备非常简单,下面以Northwind数据库为例,
step1:
step2:
step3:
确定后选择脚本保存的路径即可,特别要注意的是在第三步中选择所需要的索引等项。
编写数据库自动部署类DBInstaller
相信读者已经迫不及待的想要测试一下刚才生成的脚本怎样才能自动部署到其他的数据库服务器上了吧,下面将从创建web安装项目开始,逐步实现数据库的自动部署,我们暂时不考虑web部署,这将会在发表后续post中。
1、创建新的solution, SetupDemo,并添加类库项目AutoInstaller
2、添加类DBInstaller.cs:下载 /Files/chwkai/自动部署.rar
DBInstaller中主要是调用osql.exe,通过sql脚本注入的方式创建数据库
3、添加安装程序类Luncher.cs:
修改Lucher::Install
2 {
3 base.Install (stateSaver);
4
5 this.InstallDB();
6
7 System.Windows.Forms.MessageBox.Show("DB Created Successfully");
8 }
新增函数InstallDB:
2 {
3 HNAS.AutoInstaller.DBInstaller dbInstall = new DBInstaller();
4 dbInstall.ServerUri = "localhost";
5 dbInstall.UserName = "sa";
6 dbInstall.Password = "sa";
7
8 System.Collections.Hashtable htTable = new System.Collections.Hashtable();
9 htTable.Add("dbTest", @"c:\db");
10
11 dbInstall.DbNames = htTable;
12 dbInstall.RunCreate();
13 }
这里默认sqlsesrver为localhost,管理员用户名和密码为sa/sa,执行后将创建一个明为dbTest的数据库,所需的创建脚本(即刚才导出的sql脚本)放在c:\db下,不需要注明脚本的文件名。
All the posts in this blog are provided "AS IS" with no warranties, and confer no rights. Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 2.5 China Mainland License.