重點介紹如何在安裝包中自動為客戶建立資料庫
步驟:
1、添加一個新項目->選擇類庫模板->命名為DBCustomAction
2、單擊項目右鍵->添加新項->選擇安裝程式類(命名為DBCustomAction.cs)
3、在伺服器總管中添加->串連到資料庫->指定使用者密碼(選擇允許儲存密碼)->資料庫選擇master
4、切換到DBCustomAction.cs的檢視狀態->將伺服器總管資料庫連接中的master.dbo拖動到designer中
5、添加一個新項sql.txt(注意要使用小寫),輸入下列sql代碼
CREATE TABLE [dbo].[MK_Employees] (
[Name] [char] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Rsvp] [int] NULL ,
[Requests] [nvarchar] (4000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY];
ALTER TABLE [dbo].[MK_Employees] WITH NOCHECK ADD
CONSTRAINT [PK_MK_Employees] PRIMARY KEY CLUSTERED
(
[Name]
) ON [PRIMARY];
(P.S:也可以直接用SqlServer匯出)
6、在sql.txt的右鍵屬性中->產生操作->內嵌資源
7、將DBCustomAction.cs切換到程式碼檢視,添加下列代碼
private string GetSql(string Name)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "."+Name);
StreamReader reader = new StreamReader(strm);
return reader.ReadToEnd();
}
catch (Exception ex)
{
Console.Write("In GetSql:"+ex.Message);
throw ex;
}
}
private void ExecuteSql(string DataBaseName,string Sql)
{
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConnection1);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
protected void AddDBTable(string strDBName)
{
try
{
ExecuteSql("master","CREATE DATABASE "+ strDBName);
ExecuteSql(strDBName,GetSql("sql.txt"));
}
catch(Exception ex)
{
Console.Write("In exception handler :"+ex.Message);
}
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
AddDBTable(this.Context.Parameters["dbname"]);
}
8、再添加一個新項目,(選擇添加到解決方案中)->項目類型為安裝項目->命名為DBCustomAction Installer
9、選擇應用程式檔案夾->添加->項目輸出->主輸出
10、在方案資源管理員中->右鍵安裝項目(DBCustomAction Installer)->視圖->使用者介面
11、選中啟動結點->添加對話方塊->文本A
12、選動文字框A->右鍵->上移一直到最頂端
13、選擇文字框A屬性->修改BannerText,(Specify Database Name)
14、修改BodyText(This dialog allows you to specify the name of the database to be created on the database server. )
15、修改EditLabel1(Name of DB),修改Edit1Porperty(CUSTOMTEXTA1),將其他Edit2,3,4的Edit(2,3,4)Visible屬性設為false;
16、在方案資源管理員中->右鍵安裝項目(DBCustomAction Installer)->視圖->自訂動作
17、選中安裝結點->添加->雙擊應用程式檔案夾->主輸出來自DBCustomAction(活動)->右鍵屬性->CustomActiveData屬性修改為/dbname=[CUSTOMTEXTA1]
18、編譯產生,OK!
注:此文章主要來自於MSDN,本來代碼使用vb.net,我用C#改寫了一下,完整URL請參考
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxwlkWalkthroughUsingCustomActionToCreateDatabaseDuringInstallation.asp
另外沒有接受客戶資料庫服務Server-Name和Password的對話方塊,我想既然可以接受DBNAME參數,那麼接收Server-Name和Password應該也不難.
-------------------------------------------------------------------------------------------------------------------------------------------------------安裝檔案如何打包SQL資料庫?安裝時會自動建SQL資料庫,並可設定資料庫名。密碼,SERVER?
項目打包總結
實現功能:根據使用者自訂指定的資料庫名及其他一些檔案的安裝如(iewebcontrols)
步驟:
建立一項目:visual C#項目 , 選中類庫模板 ,如命名為myInstall,在此項目下面添加一新項:選擇安裝程式類(Installer1.cs),在此我採用預設名。此安裝類庫將實現資料庫的安裝及其他一些檔案的安裝。
在installer1.cs裡我們將重寫安裝類的方法:見參考1
Install Commit Rollback Uninstall
在install方法中來實現自訂的安裝:
首先我們要得到一些使用者變數(使用者在安裝時所做的操作,如資料庫管理帳號等)
對於這些變數我們可以通過上下文環境來得到this.Context.Parameters["DataBaseName"];(在安裝項目裡將會進行設定)
在此我們一共得到五個變數。安裝資料庫的伺服器,指定的資料庫名,資料庫管理員帳號,密碼。以及Database Backup檔案。
我們來看一下具體的代碼,很簡單:)
private void InstallDateBase()
{
System.Reflection.Assembly Asm;
// Asm= System.Reflection.Assembly.GetExecutingAssembly();
// System.IO.FileInfo FileInfo = new System.IO.FileInfo(Asm.Location);
// 也可以得到當前安裝的目錄,如wwwroot目錄下myweb/你的安裝工程名.dll
//下面是進行資料庫連線及命令
SqlConnection sqlConnection1 = new SqlConnection();
string DBName = this.Context.Parameters["DataBaseName"];
string DBServer = this.Context.Parameters["ServerName"];
string DBUser = this.Context.Parameters["InStallAdmin"];
string DBKey = this.Context.Parameters["InstallPassword"];
string dir=this.Context.Parameters["dir"];
StreamWriter ss=new StreamWriter(@"c:/aaa.txt",true);
ss.WriteLine("data source=" + DBName + ";database=master;uid=" + DBUser +";password=" + DBKey);
ss.Close();
string CONSTR = "data source=" + DBServer +";database=master;uid=" + DBUser +";password=" + DBKey;
sqlConnection1.ConnectionString = CONSTR;
//資料庫將安裝到system32下
string CurDir = Directory.GetCurrentDirectory() + @"/TestData/database";
if(!Directory.Exists(CurDir))//不存在就建立一個
{
Directory.CreateDirectory(CurDir);
}
string MSQL = "RESTORE DATABASE " + DBName +
" FROM DISK = '" + dir + @"/database/myDataBase.bak' " +
" WITH MOVE 'myDate_dat' TO '" + CurDir + @"/" + DBName + ".mdf', " +
" MOVE 'myDate_log' TO '" + CurDir + @"/" + DBName + ".ldf' " ;
SqlCommand cmd = new SqlCommand(MSQL,sqlConnection1);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
if(sqlConnection1.State == ConnectionState.Open)
{
sqlConnection1.Close();
}
//安裝TreeView
ProcessStartInfo psi = new ProcessStartInfo();
psi.WorkingDirectory = dir+"//database";
psi.FileName = dir + @"/database/iewebcontrols.msi ";
psi.UseShellExecute=true; //msi檔案,如是exe不用設
Process.Start(psi);
//有哪位兄弟幫忙看一下。下面的代碼。在執行時會說有一個進程在運行,導致安裝這些控制項時無法運行。一定要關了。才可以.但是下面的代碼比上面的代碼有一點好處,下面的進程等到結束後繼續整個install方法的其他代碼。而上面的代碼,在實際運行時,整個安裝已結束了。而安裝TreeView的工作才剛開始。有點不同步
// System.Diagnostics.Process myproc=new System.Diagnostics.Process();
// myproc.EnableRaisingEvents=true;
//
// string file=dir+@"/database/iewebcontrols.msi";
//
// myproc.StartInfo.UseShellExecute=true;
// myproc.StartInfo.FileName = file;
// myproc.Start();
// myproc.WaitForExit();//此方法可以無限期等到進程運行完成,才執行下面的代碼
// if(myproc.HasExited==true)
// {
// // myproc.Kill();
//
// }
}
public override void Install(System.Collections.IDictionary stateSaver)
{
try
{
base.Install(stateSaver);
this.InstallDateBase();//調用上面的方法
}
catch
{
throw;
}
}
public override void Uninstall(System.Collections.IDictionary stateSaver)
{
base.Uninstall(stateSaver);
//UnInstallDataBase();
}
public override void Commit(System.Collections.IDictionary stateSaver)
{
base.Commit(stateSaver);
}
public override void Rollback(System.Collections.IDictionary stateSaver)
{
base.Rollback(stateSaver);
}
參考1:
ms-help://MS.VSCC/MS.MSDNVS.2052/Vbcon/html/vbtskoverridingdefaultmethodsoninstallationcomponent.htm
ms-help://MS.VSCC/MS.MSDNVS.2052/vsintro7/html/vxwlkWalkthroughUsingCustomActionToCreateDatabaseDuringInstallation.htm
現在我們再此工程下面添加一個新的項目
安裝和部署項目,選中Web 專案模板,如命名為websetup2.
建立部署項目
1. 在[檔案] 功能表上,選擇“添加項目”->“建立項目”。
2. 在“添加項目”對話方塊中,選擇“項目類型”窗格中的“安裝和部署項目”,然後選擇“模板”窗格中的“WEB安裝項目”。在“名稱”框中,鍵入 websetup2。(我是預設的,沒改。這些語句懶得打,抄來的,:))
3. 在“屬性”視窗中,選擇 ProductName 屬性並鍵入 mywebTest。
4. 在檔案系統編輯器中,選擇“應用程式檔案夾”。在“操作”菜單上,選擇“添加”->“項目輸出”。
5. 在“添加項目輸出組”對話方塊中,為“myInstall”項目選擇主輸出。
在前面的操作與一般打包一樣,將你的WEB工程裡的一些檔案添加進來。及一些其他打包設定(產品名等),我就不多說了。
我們這裡僅僅要加入一個Database Backup檔案(myDataBase.bak)
首先開啟websetup2的檔案系統視窗,選中web應用程式檔案夾,在此檔案夾下面添加一個檔案夾我們用來存放資料庫,如database,再將mydatabase.bak檔案複製到此目錄下面。
現在我們要添加一個安裝時的視窗,用來讓使用者輸入資料庫帳號等。我們只要開啟使用者介面視窗就可以進行操作了。
建立自訂安裝對話方塊
1. 在方案總管中選擇“websetup2”項目。在“視圖”菜單上指向“編輯器”,然後選擇“使用者介面”。
2. 在使用者介面編輯器中,選擇“安裝”下的“啟動”節點。在“操作”菜單上,選擇“添加對話方塊”。
3. 在“添加對話方塊”對話方塊中選擇“文字框 (A)”對話方塊。
4. 在“操作”菜單上選擇“上移”。重複此步驟,直到“文字框 (A)”對話方塊位於“安裝資料夾”節點之上。
5. 在“屬性”視窗中,選擇 BannerText 屬性並鍵入 自訂資料庫安裝
6. 選擇 BodyText 屬性並鍵入自訂資料庫安裝
7. 選擇 Edit1Label 屬性並鍵入伺服器名:。
8. 選擇 Edit1Property 屬性並鍵入 SERVERNAME。
9. 選擇 Edit2Label 屬性並鍵入建立資料庫名:。
10. 選擇 Edit2Property 屬性並鍵入 DATABASENAME。
11. 選擇 Edit3Label 屬性並鍵入資料庫管理員帳號:。
12. 選擇 Edit3Property 屬性並鍵入 INSTALLADMIN。
13. 選擇 Edit4Label 屬性並鍵入管理員密碼:。INSTALLPASSWORD
14. 選擇 Edit4Property 屬性並鍵入 SERVERNAME。
建立自訂動作
1. 在方案總管中選擇“websetup2”項目。在“視圖”菜單上指向“編輯器”,然後選擇“自訂動作”。
2. 在自訂動作編輯器中選擇“安裝”節點。在“操作”菜單上,選擇“添加自訂動作”。
3. 在“選擇項目中的項”對話方塊中,雙擊“應用程式檔案夾”。
4. 選擇“主輸出來自 DBCustomAction(活動)”項。
在“屬性”視窗中,選擇 CustomActionData 屬性並鍵入 /InstallPassword=[INSTALLPASSWORD] /InStallAdmin=[INSTALLADMIN] /ServerName=[SERVERNAME] /DataBaseName=[DATABASENAME] /dir="[TARGETDIR]/"
注意各個 使用者資料之間是有空格的。如/aaa=[fdfad] /bfff=[fdafdfd]
5. 在“產生”菜單上選擇“產生websetup2”。
上面的customActionData將會在安裝類中引用的。注意各個之間一定要有空格。
/dir=[“TARGETDIR”],取的是最終安裝的WEB目錄
到此一個項目打包就此結束了。