天天看點

.net打包自動安裝資料庫

一).建立部署項目

  1. 在“檔案”菜單上指向“添加項目”,然後選擇“建立項目”。

  2. 在“添加新項目”對話框中,選擇“項目類型”窗格中的“安裝和部署項目”,然後選擇“模闆”窗格中的“安裝項目”。在“名稱”框中鍵入 setup1。

  3. 單擊“确定”關閉對話框。

  4. 項目被添加到解決方案資料總管中,并且檔案系統編輯器打開。

  5. 在“屬性”視窗中,選擇 productname 屬性,并鍵入 資訊管理系統 。

  二).将 主程式 項目的輸出添加到部署項目中

  1. 在“檔案系統編輯器”中,選擇“應用程式檔案夾”。在“操作”菜單上,指向“添加”,然後選擇“項目輸出”。

  2. 在“添加項目輸出組”對話框中,選擇“項目”下拉清單中的“你的程式”。

  4. 從清單中選擇“主輸出”和“内容檔案”組,然後單擊“确定”。

  三).建立安裝程式類

  1. 在“檔案”菜單上指向“建立”,然後選擇“項目”。

  2. 在“建立項目”對話框中,選擇“項目類型”窗格中的“visual basic 項目”,然後選擇“模闆”窗格中的“類庫”。在“名稱”框中鍵入 installdb。

  3. 單擊“打開”關閉對話框。

  4. 從“項目”菜單中選擇“添加新項”。

  5. 在“添加新項”對話框中選擇“安裝程式類”。在“名稱”框中鍵入 installdb。

  6. 單擊“确定”關閉對話框。

  7. 詳細代碼附後。

  四).建立自定義安裝對話框

  1. 在解決方案資料總管中選擇“setup1”項目。在“視圖”菜單上指向“編輯器”,然後選擇“使用者界面”。

  2. 在使用者界面編輯器中,選擇“安裝”下的“啟動”節點。在“操作”菜單上,選擇“添加對話框”。

  3. 在“添加對話框”對話框中,選擇“許可協定”對話框,然後單擊“确定”關閉對話框。

  4. 在“添加對話框”對話框中,選擇“文本框 (a)”對話框,然後單擊“确定”關閉對話框。

  5. 在“操作”菜單上,選擇“上移”。重複此步驟,直到“文本框 (a)”對話框位于“安裝檔案夾”節點之上。

  6. 在“屬性”視窗中,選擇 bannertext 屬性并鍵入:安裝資料庫.

  7. 選擇 bodytext 屬性并鍵入:安裝程式将在目标機器上安裝資料庫

  8. 選擇 edit1label 屬性并鍵入:資料庫名稱:

  9. 選擇 edit1property 屬性并鍵入 customtexta1

  10. 選擇 edit1value 屬性并鍵入:dbservers

  11. 選擇 edit2label 屬性并鍵入:伺服器名:

  12. 選擇 edit2property 屬性并鍵入 customtexta2

  13. 選擇 edit2value 屬性并鍵入:(local)

  14. 選擇 edit3label 屬性并鍵入:使用者名:

  15. 選擇 edit3value 屬性并鍵入:sa

  16. 選擇 edit3property 屬性并鍵入 customtexta3

  17. 選擇 edit4label 屬性并鍵入:密碼:

  18. 選擇 edit4property 屬性并鍵入 customtexta4

  19. 選擇 edit2visible、edit3visible 和 edit4visible 屬性,并将它們設定為 true

  五).建立自定義操作

  1. 在解決方案資料總管中選擇“setup1”項目。在“視圖”菜單上指向“編輯器”,然後選擇“自定義操作”。

  2. 在自定義操作編輯器中選擇“安裝”節點。在“操作”菜單上,選擇“添加自定義操作”。

  3. 在“選擇項目中的項”對話框中,輕按兩下“應用程式檔案夾”。

  4. 選擇“主輸出來自 installdb(活動)”項,然後單擊“确定”關閉對話框。

  5. 在“屬性”視窗中,選擇 customactiondata 屬性并鍵入“/dbname=[customtexta1] /server=[customtexta2] /user=[customtexta3] /pwd=[customtexta4] /targetdir="[targetdir]/"”。

  附:/targetdir="[targetdir]/"是安裝後的目标路徑,為了在installdb類中獲得安裝後的路徑,我們設定此參數。

  六).打包時加入解除安裝功能:

  方法一:

  1.在打包項目中添加檔案msiexec.exe(一般可在c:/windows/system32/下找到)

  2.在檔案系統視圖中選擇應用程式檔案夾,在msiexec.exe上按右鍵,選擇創建快捷方式,重命名快捷方式為"解除安裝".

  3.更改此快捷方式的arguments 為"/x {產品id}",產品id的值為打包項目的productcode屬性值.

  方法二:(推薦)

  1.先生成安裝包,記下productcode(選擇解決方案資料總管根目錄如setup1,再檢視屬性标簽,不是右鍵中的屬性),下面要用到

  2.用vs.net建立一個新的控制台程式uninst.exe檔案

'power by: landlordh

'for 2000,xp,2003

module uninstall

    sub main()

        dim myprocess as process = new process

        if system.environment.osversion.tostring.indexof("nt 5") then

            myprocess.start("msiexec", "/x{2b65d4a9-c146-4808-ab4b-321fb0779559}")  '

改為自己的productcode

        end if

        myprocess.close()

    end sub

end module

3.将控制台程式bin目錄的exe檔案加入到打包程式檔案中,在程式組建立uninst.exe的快捷方式

installdb.vb類,要添加引用 system.configuration.install.dll :

using system;

using system.collections;

using system.componentmodel;

using system.configuration.install;

using system.reflection;

using system.io;

using system.data;

using system.data.sqlclient;

namespace install

{

 /// <summary>

 /// installer1 的摘要說明。

 /// </summary>

 [runinstaller(true)]

 public class installer1 : system.configuration.install.installer

 {

  /// <summary>

  /// 必需的設計器變量。

  /// </summary>

  private system.componentmodel.container components = null;

  public installer1()

  {

   // 該調用是設計器所必需的。

   initializecomponent();

   // todo: 在 initializecomponent 調用後添加任何初始化

  }

  /// 清理所有正在使用的資源。

  protected override void dispose( bool disposing )

   if( disposing )

   {

    if(components != null)

    {

     components.dispose();

    }

   }

   base.dispose( disposing );

  #region 元件設計器生成的代碼

  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改

  /// 此方法的内容。

  private void initializecomponent()

   components = new system.componentmodel.container();

  #endregion

  private  string getsql(string name) 

  { 

//   //調用osql執行腳本

//

//   system.diagnostics.process sqlprocess = new system.diagnostics.process();

//   sqlprocess.startinfo.filename = "osql.exe";

//   sqlprocess.startinfo.arguments = string.format(" -u {0} -p {1} -d {2} -i {3}db.sql", this.context.parameters["user"], this.context.parameters["pwd"],"master", this.context.parameters["targetdir"]);

//   sqlprocess.startinfo.windowstyle = system.diagnostics.processwindowstyle.hidden;

//   sqlprocess.start();

//   sqlprocess.waitforexit() ;//等待執行

//   sqlprocess.close();

   try 

   { 

//    assembly asm = assembly.getexecutingassembly();

//    system.io.fileinfo fileinfo = new system.io.fileinfo(asm.location);

//    string path=fileinfo.directoryname+@"/"+name;

    string path=this.context.parameters["targetdir"]+name;

    filestream fs=new filestream(path,filemode.open,fileaccess.read,fileshare.read);

    streamreader reader = new streamreader(fs,system.text.encoding.default); 

    //system.text.encoding.ascii;

    return reader.readtoend(); 

   } 

   catch (exception ex) 

    console.write("in getsql:"+ex.message); 

    throw ex; 

  } 

  private void executesql(string databasename,string sql) 

   sqlconnection sqlconnection1=new sqlconnection();

   sqlconnection1.connectionstring =string.format("server={0}; user id={1}; password={2}; database=master",this.context.parameters["server"],this.context.parameters["user"],this.context.parameters["pwd"]);

   system.data.sqlclient.sqlcommand command = new system.data.sqlclient.sqlcommand(sql,sqlconnection1); 

    command.connection.open(); 

    command.connection.changedatabase(databasename); 

    command.executenonquery(); 

   catch(exception ex) 

    console.write("in exception handler :"+ex.message); 

   finally 

    command.connection.close(); 

protected void adddbtable(string strdbname) 

   {     

    executesql("master","create database "+ strdbname);

    executesql(strdbname,getsql("sql.txt")); 

    executesql("master","exec sp_addlogin 'myoamaster','myoamaster','"+strdbname+"',null,null");

    executesql(strdbname,"exec sp_grantdbaccess 'myoamaster', 'myoamaster'");

    executesql(strdbname,"exec sp_addrolemember 'db_owner','myoamaster'");

  public override void install(system.collections.idictionary statesaver) 

   base.install(statesaver); 

   adddbtable(this.context.parameters["dbname"]); 

 }

}

  這裡有個sql.txt是資料庫的sql腳本,當然可以調用osql來執行sql腳本,其實是一樣的。

  打包的時候必須把sql.txt檔案加進來,否則不會執行。

  

  如果你想附加資料庫的mdf檔案和ldf檔案,用下面這段程式:

private void createdatabase(string strsql,string dataname,string strmdf,string strldf)

   string str;

   sqlconnection myconn = new sqlconnection (strsql);

   //exec sp_detach_db @dbname = 'bx_freightmileage_2'//需要先将資料庫分離出來

   str = "exec sp_attach_db @dbname = '"+ dataname +"', @filename1 = '"+ strmdf +"',@filename2='"+strldf+"'";

   sqlcommand mycommand = new sqlcommand(str, myconn);

    myconn.open();

    mycommand.executenonquery();

    myconn.close();

  當然打包的時候也要把這兩個資料庫檔案也加進來。