在.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 的摘要說明。
///@ author:overred
/// </summary>
[RunInstaller(true)]
public class DBCustomAction : System.Configuration.Install.Installer
{
/// <summary>
/// 必需的設計器變量。
/// </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("http://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
}
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
