简单的ASP.NET部署,运行环境:vs2003,SqlServer2000
(一)前提:
http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=627921a0-d9e7-43d6-a293-72f9c370bd19
下载"Microsoft Visual Studio .NET 2003 引导程序插件",它用于在 Microsoft® Visual Studio® .NET 2003 中创建包含 .NET Framework 1.1 版和/或 Microsoft Data Access Components (MDAC) 2.7 版的部署项目。
(二)建立简单的asp.net项目WebSetupTest
1.web.config文件里添加保存数据库连接字符串的key,部署的时候将初始化它.
<add key="Conn_WebSetupTest" value=""></add>
</appSettings>
2.
创建index.aspx文件
测试部署数据库和开始菜单/桌面快捷方式
<br>
<asp:DataGrid id="DataGrid1" runat="server" Width="100%" AllowPaging="True" PageSize="5">
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:DataGrid><br>
<p align="center"><img src="images/images_res.gif"></p>
</form>
index.aspx.cs里绑定数据
{
connE.strSql = "select * from users order by id desc";
DataGrid1.DataSource = connE.GetDt();
DataGrid1.DataBind();
}
3.项目根目录下创建SqlScript目录用来存放部署数据需要的3个数据库脚本文件
(1)CreateDataBase.sql用来创建数据库 --创建数据库<<DATABASE_NAME>>
Go
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = '<<DATABASE_NAME>>')
DROP DATABASE [<<DATABASE_NAME>>]
GO
Create Database <<DATABASE_NAME>>
Go
use <<DATABASE_NAME>>
--创建表
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Users]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Users]
GO
CREATE TABLE [dbo].[Users] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[address] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[createDate] [datetime] NULL
) ON [PRIMARY]
GO
(2)InsertData.sql用来向数据库里插入记录
declare @i int
set @i= 0
set xact_abort on
begin transaction
while @i<50
begin
insert into users (name,address,createdate) values ('name'+cast(@i as varchar),'address'+cast(@i as varchar),getDate())
set @i = @i+1
end
--rollback transaction
commit transaction
set xact_abort off
(3)DropDataBase.sql用来卸载的时候删除数据库.
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = '<<DATABASE_NAME>>')
DROP DATABASE [<<DATABASE_NAME>>]
4.把快捷方式图标文件usa_folder_dialup.ico放在项目根目录下的确images目录里.
(三)在同一解决方案里添加"类库"项目WebSetupLib
1.把存放数据库脚本文件名称的DataBase.xml和用户许可协议文件UserProtocol.rtf放在Resources目录下.
UserProtocol.rtf文件可以通过Word直接创建,DataBase.xml要作为嵌入式资源,设置方法如图:
DataBase.xml内容:
<configroot>
<Files>
<DataBase>
<Add>
<File name="CreateDataBase.sql"/>
</Add>
<Remove>
<File name="DropDataBase.sql"/>
</Remove>
</DataBase>
<Insert>
<File name="InsertData.sql"/>
</Insert>
</Files>
</configroot>
2.添加部署数据库的类文件DataBase.cs,在下面的安装程序类文件WebSetupTest.cs里将使用此类
sing System;
using System.Diagnostics;
using System.Configuration.Install;
using System.Xml ;
using System.Windows.Forms;
namespace WebSetupLib
{
/**//// <summary>
/// DataBase 的摘要说明。
/// </summary>
public class DataBase
{
string serverName=null;
string databaseName =null;
string userName=null;
string Password=null;
bool trustedconnection=false;
string targetPath=null;
XmlDocument config=null;
public const string CONST_DATBASE_PLACEHOLDER ="<<DATABASE_NAME>>";
//构造方法#region //构造方法
public DataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
//System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("WebSetupLib.DataBase.xml");
System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("WebSetupLib.Resources.DataBase.xml");
config=new XmlDocument();
//MessageBox.Show("before load1111");
config.Load(stream);
//MessageBox.Show("after load 111");
}
public DataBase(string servername,string databasename,string username,
string password,string targetpath):this()
{
this.serverName =servername;
this.databaseName=databasename;
this.userName=username;
this.Password=password;
this.targetPath=targetpath;
}
/**//// <summary>
/// 信任连接
/// </summary>
/// <param name="servername"></param>
/// <param name="databasename"></param>
/// <param name="targetpath"></param>
public DataBase(string servername,string databasename,string targetpath):this()
{
this.serverName =servername;
this.databaseName=databasename;
this.trustedconnection=true;
this.targetPath =targetpath;
}
#endregion
/**//// <summary>
/// 安装数据库
/// </summary>
/// <returns></returns>
public bool CreateDataBase()
{
string fileName=null;
try
{
ProcessStartInfo processInfo =new ProcessStartInfo("osql.exe");
processInfo.WindowStyle=ProcessWindowStyle.Hidden;
//Get the name of the file from the assembly's embedded resource.
//MessageBox.Show("createDatabase");
if(config !=null)
{
fileName = config.SelectSingleNode("configroot/Files/DataBase/Add/File").Attributes["name"].Value;
}
else
{
throw new InstallException("创建数据库的脚本文件不存在!");
}
//Get arguments
//MessageBox.Show("master");
processInfo.Arguments=GetCommonProcessArguments(fileName,"master");
EventLog.WriteEntry("安装数据库",processInfo.Arguments);
PopulateDatabaseNamePlaceHolder(GetFullPath(fileName));
Process osql = Process.Start(processInfo);
//Wait till it is done
osql.WaitForExit();
EventLog.WriteEntry( "安装数据库","数据库创建完成..");
osql.Dispose();
return true;
}
catch(Exception ex)
{
//Customize if required.
EventLog.WriteEntry("安装数据库",ex.Message,EventLogEntryType.Error) ;
throw new InstallException(ex.Message);
}
}
/**//// <summary>
/// 删除数据库
/// </summary>
/// <returns></returns>
public bool DropDataBase()
{
string fileName=null;
try
{
ProcessStartInfo processInfo =new ProcessStartInfo("osql.exe");
processInfo.WindowStyle=ProcessWindowStyle.Hidden;
//Get the name of the file from the assembly's embedded resource.
if(config !=null)
{
fileName = config.SelectSingleNode("configroot/Files/DataBase/Remove/File").Attributes["name"].Value;
}
else
{
throw new InstallException("删除数据库的脚本文件不存在!");
}
//Get arguments
processInfo.Arguments=GetCommonProcessArguments(fileName,"master");
EventLog.WriteEntry("安装数据库",processInfo.Arguments);
PopulateDatabaseNamePlaceHolder(GetFullPath(fileName));
Process osql = Process.Start(processInfo);
osql.WaitForExit();
EventLog.WriteEntry("安装数据库","删除数据库");
osql.Dispose();
return true;
}
catch(Exception ex)
{
//Customize if required.
EventLog.WriteEntry("安装数据库",ex.Message,EventLogEntryType.Error) ;
throw new InstallException(ex.Message);
}
}
/**//// <summary>
/// 插入数据
/// </summary>
/// <returns></returns>
public bool InsertDate()
{
try
{
ExecuteScripts("configroot/Files/Insert/File");
EventLog.WriteEntry("安装数据库","记录增加完成");
return true;
}
catch(Exception ex)
{
//Customize if required.
EventLog.WriteEntry("安装数据库",ex.Message,EventLogEntryType.Error) ;
throw new InstallException(ex.Message);
}
}
/**//// <summary>
/// 执行数据库脚本
/// </summary>
/// <param name="Xpath"></param>
void ExecuteScripts(string Xpath)
{
XmlNodeList objectlist=null;
ProcessStartInfo processInfo =new ProcessStartInfo("osql.exe");
processInfo.WindowStyle=ProcessWindowStyle.Hidden;
//Get the name of the file from the assembly's embedded resource.
if(config !=null)
{
objectlist = config.SelectNodes(Xpath);
}
else
{
throw new InstallException("保存数据库脚本文件名称的XML文件不存在!");
}
foreach(XmlNode objectFile in objectlist)
{
//Get arguments
processInfo.Arguments=GetCommonProcessArguments(objectFile.Attributes["name"].Value,databaseName);
EventLog.WriteEntry("安装数据库",processInfo.Arguments);
Process osql = Process.Start(processInfo);
//Wait till it is done
osql.WaitForExit();
EventLog.WriteEntry( "安装数据库", objectFile.InnerText +" file executed..");
}
}
/**//// <summary>
/// 获得数据库脚本的完整路径
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private string GetFullPath(string fileName)
{
//The destination folder for this will be the Install\scripts folder under the
//installation root
//MessageBox.Show(targetPath + @"SqlScript\" + fileName);
string FullPath = targetPath + @"SqlScript\" + fileName;
return FullPath;
}
/**//// <summary>
/// 读数据库脚本文件的内容
/// </summary>
/// <param name="filepath"></param>
void PopulateDatabaseNamePlaceHolder(string filepath)
{
//Read the contents
System.IO.StreamReader reader = new System.IO.StreamReader(filepath);
string content=reader.ReadToEnd();
reader.Close();
//Replace the placeholder with database name
content =content.Replace(CONST_DATBASE_PLACEHOLDER,databaseName);
//Writeback the contents
System.IO.StreamWriter writer = new System.IO.StreamWriter(filepath,false);
//MessageBox.Show(content);
writer.Write(content);
writer.Flush();
writer.Close();
}
string GetCommonProcessArguments(string fileName, string overridendatabasename)
{
//是否是信任连接
if(!trustedconnection)
{
/**//* Arguments configured.
* osql [-S server] [-d use database name] [-U login id] [-P password]
[-i inputfile] [-o outputfile]
*/
return " -S " + serverName + " -d " + overridendatabasename +" -U " + userName + " -P " +Password + " -i " + Char.ToString('"') + GetFullPath(fileName) + Char.ToString('"') + " -o " + Char.ToString('"') + targetPath + "Log.txt" + Char.ToString('"');
}
else
{
/**//* Arguments configured.
* osql [-S server] [-d use database name] [-E trusted connection]
[-i inputfile] [-o outputfile]
*/
return " -S " + serverName + " -d " + overridendatabasename + " -E " + " -i " + Char.ToString('"') + GetFullPath(fileName) + Char.ToString('"') + " -o " + Char.ToString('"') + targetPath + "Log.txt" + Char.ToString('"');
}
}
}
}
asp.net 部署数据库、开始菜单、桌面快解方式实例(下)
http://adandelion.cnblogs.com/archive/2006/02/12/329132.html
http://www.chinapoesy.com
诗词在线 |唐诗|宋词|元曲|现代诗歌|外国诗歌
126在线阅读网
http://www.Read126.cn
126在线阅读网 人物传记、古典名著、历史书籍。。。