目的:在用戶端伺服器上”附加資料庫檔案”。
一).建立部署項目
1. 開啟VS.NET2005。
2.在[檔案] 功能表上指向“建立項目”。
3. 在“建立項目”對話方塊中,選擇“項目類型”窗格中的”其他項目類型”中的“安裝和部署”,然後選擇“模板”窗格中的“安裝項目”。在“名稱”框中鍵入 Setup1。
4. 單擊“確定”關閉對話方塊。
5. 項目被添加到方案總管中,並且檔案系統編輯器開啟。如所示:
6. 在“屬性”視窗中,選擇 ProductName 屬性,並鍵入 資料庫打包安裝。
二).建立安裝程式類
1. 在“方案總管”中的”解決方Setup1”上右鍵選擇”添加”後選擇“建立項目”。
2. 在“添加新項目”對話方塊中,選擇“項目類型”窗格中的“Visual C#”下的”Windows”,然後選擇“模板”窗格中的“類庫”。在“名稱”框中鍵入 InstallDB。
3. 單擊“確定”關閉對話方塊。
4. 從“InstallDB”類庫下右鍵選擇”添加”中的”建立項”。
5. 在“添加新項”對話方塊中選擇“安裝程式類”。在“名稱”框中鍵入 InstallDB。
6. 單擊“添加”關閉對話方塊。
7. “InstallDB”安裝程式類詳細代碼附後。
如所示:
三).建立自訂安裝對話方塊
1. 在方案總管中選擇“setup1”項目。右鍵然後在“視圖”菜單上選擇“使用者介面”。
2. 在使用者介面編輯器中,選擇“安裝”下的“啟動”節點選擇“添加對話方塊”。
3 在“添加對話方塊”對話方塊中,選擇“文字框 (A)”對話方塊,然後單擊“確定”關閉對話方塊。
4. 在“文字框 (A)”右鍵菜單上,選擇“上移”。重複此步驟,直到“文字框 (A)”對話方塊位於“安裝資料夾”節點之上。
5 在“文字框 (A)”的“屬性”視窗中,選擇 BannerText 屬性並鍵入:安裝資料庫。
6. 選擇 BodyText 屬性並鍵入:安裝程式將在目標機器上安裝資料庫。
7. 選擇 Edit1Label 屬性並鍵入:資料庫名稱:
8 選擇 Edit1Property 屬性並鍵入 DBNAME
9. 選擇 Edit1Value 屬性並鍵入: budgetSysDB
10. 選擇 Edit2Label 屬性並鍵入:伺服器名:
11. 選擇 Edit2Property 屬性並鍵入 SERVER
12. 選擇 Edit2Value 屬性並鍵入:(local)
13. 選擇 Edit3Label 屬性並鍵入:使用者名稱:
13. 選擇 Edit3Value 屬性並鍵入:sa
15. 選擇 Edit3Property 屬性並鍵入 USER
16. 選擇 Edit4Label 屬性並鍵入:密碼:
18. 選擇 Edit4Property 屬性並鍵入 PWD
19. 選擇Edit2Visible 屬性,設定為 false;選擇Edit2Visible、Edit3Visible 和 Edit4Visible 屬性,並將它們設定為 true
四).建立自訂動作
1. 在方案總管中選擇“setup1”項目,右鍵在“視圖”菜單上選擇“自訂動作”。
2. 在自訂動作編輯器中選擇“安裝”節點。右鍵選擇“添加自訂動作”。
3. 在“選擇項目中的項”對話方塊中,雙擊“應用程式檔案夾”。
4. 單擊”添加輸出”按鈕選擇”項目”中的” InstallDB”,在下面框中選擇”主輸出”(此項一般為預設),然後單擊“確定”關閉對話方塊。
5. 在”主輸出來自InstallDB(活動)的屬性視窗中,選擇 CustomActionData屬性並鍵入:/dbname=[DBNAME] /server=[SERVER]/user=[USER]/pwd=[PWD]/targetdir="[TARGETDIR]\"。
附:/targetdir="[TARGETDIR]\"是安裝後的目標路徑,為了在installDB類中獲得安裝後的路徑,我們設定此參數。
如所示:
五).添加資料庫檔案
1. 在方案總管中選擇“Setup1”項目,右鍵在“添加”選擇“檔案”。
2.把資料檔案”budgetSysDB_Data.MDF”” budgetSysDB_Log.LDF”” budgetDB”添加到” Setup1”。
如所示:
六).”InstallDB”安裝程式類代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Data.SqlClient;
namespace InstallDB
{
[RunInstaller(true)]
public partial class InstallerDB : Installer
{
public InstallerDB()
{
InitializeComponent();
}
private void CreateDataBase(string strSql, string DataName, string strMdf, string strLdf,string path,string strSql1)
{
### 附加資料庫#region ### 附加資料庫
String str;
SqlConnection myConn = new SqlConnection(strSql);
str = "EXEC sp_attach_db @dbname = '" + DataName + "', @filename1 = '" + strMdf + "',@filename2='" + strLdf + "'";
SqlCommand myCommand = new SqlCommand(str, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
#endregion
###建立資料庫使用者的登陸名和密碼(登陸名:ys2006 密碼:zhouhuan965)#region ###建立資料庫使用者的登陸名和密碼(登陸名:ys2006 密碼:zhouhuan965)
string str2 = "exec sp_addlogin 'ys2006','zhouhuan965'" + " " + "use " + DataName + "" + " " + "exec sp_adduser 'ys2006'" + " " + "exec sp_addrolemember 'db_owner','ys2006' ";
SqlCommand cmd2 = new SqlCommand(str2, myConn);
cmd2.ExecuteNonQuery();
myConn.Close();
#endregion
###把使用者安裝路徑存入資料庫#region ###把使用者安裝路徑存入資料庫
SqlConnection cn = new SqlConnection(strSql1);
string str3 = "insert into T_DataPath (DataPath) values('" + path + "')";
cn.Open();
SqlCommand cmd3 = new SqlCommand(str3, cn);
cmd3.ExecuteNonQuery();
cn.Close();
#endregion
}
public override void Install(System.Collections.IDictionary stateSaver)
{
string server = this.Context.Parameters["server"];
string uid = this.Context.Parameters["user"];
string pwd = this.Context.Parameters["pwd"];
string path = this.Context.Parameters["targetdir"];
string strSql = "server=" + server + ";uid=" + uid + "; pwd=" + pwd + ";database=master";
string strSql1 = "server=" + server + ";uid=" + uid + "; pwd=" + pwd + ";database=budgetSysDB";
string DataName = "budgetSysDB";
string strMdf = path + "\\budgetSysDB_Data.MDF"; //+ @"\" + "+budgetDB1.mdf+";
string strLdf = path + "\\budgetSysDB_Log.LDF"; //+@"\" + "+budgetDB1.ldf+";
base.Install(stateSaver);
this.CreateDataBase(strSql, DataName, strMdf, strLdf,path,strSql1);//調用上面的方法
}
}
}