基於4.5Framework web程序、SQLSERVER數據庫打包


  估計很多朋友和我一樣,對於C/S程序打包很熟悉,但對於B/S程序打包一頭霧水。。。

  最近公司要求我們把項目和數據庫(SQLSERVER)一起打包,然后安裝在CD光盤上,打算拿光盤去客戶那邊實現一鍵安裝。哎!!!最終這個任務給我了,

我只有抱着學習的態度去慢慢摸索。

  打包的程序是基於VS2012、MVC4模板開發出來的,框架是4.5版本。

  類似於這種4.5框架打包,目前微軟好像也提供了一個打包工具 InstallShield,下載InstallShield2012 或者InstallShield2013,然后安裝,安裝完成之后就會

安裝和部署里面會生成一個圖標(InstallShield Limited Edition Project), 如下圖(1.1)。。

           

                                  圖(1.1)

 通過雙擊 InstallShield Limited Edition Project這個圖標,就可以根據前進后退圖標實現安裝了,如圖(1.2)

               

                                圖(1.2)

最終WEB打包生成出來的只是一個lnk快捷方式圖標,無法指定到輸出文件。然后我用C/S WINFORM程序、也是基於4.5框架來進行打包,發現安裝生成出來的

文件可以完美運行。

產生這種情況我也感到很無語,於是在網上查找這方面的相關資料,發現InstallShield這款插件從2010開始,微軟已經不管了,只是推薦這款插件。

哎!!!真坑...

基於這種情況我徹底無語了,只能從新找其他辦法。

之后我就開始嘗試把4.5框架程序降到4.0框架程序,然后再次進行打包。

降框架有兩種辦法:1.在VS2012上面打開, 更改所有程序集的目標框架,如圖(1.3)

                 

                               如圖(1.3)

       2. 用文本打開項目文件進行更改,更改TargetFrameworkVersion 為v4.0 如圖(1.4)

                   

                                                                                    圖(1.4)

 但是如果在4.5框架程序中引用了4.5版本的包,那也要對包進行降版本。去掉引用的包,或者找低版本的包來替代高版本的包

下面開始講解用VS2010打包 4.0WEB程序

          1.用VS2010打開即將要打包的WEB程序(圖就省了。。)

          2.右擊解決方案一>添加一>新建項目,然后點擊web安裝項目,如圖(1.5)

         

                            圖(1.5)

     3.右擊安裝包WebSetup1一>添加一>項目輸出,然后選中項目WEB程序集 ,選中本地化資源、內容文件  如圖(1.6)、(1.7)

    

                                                                         圖(1.6)

         

                                                                                圖(1.7)

          4.右擊安裝包一>視圖一>用戶界面  ,如圖(1.8)

    

                                                                         圖(1.8)

            5.右擊啟動一>添加對話框一>選中文本框、許可協議,如圖(1.9)

    

                        圖(1.9)

            6.右擊文本框(A),許可協議上移到選定的位置, 如圖(2.0)

    

                    圖(2.0)

      7.新建一個許可協議文件,打開WORD文檔,里面寫寫協議內容,保存后WORD文檔要改成rtf格式,然后右擊許可協議一>屬性窗口,在LicenseFile屬性中點擊瀏覽,如圖(2.1)

        

                      如圖(2.1)

         8.雙擊Web應用程序文件夾,添加rtf文件,如圖(2.2)

      

                          圖(2.2)

     9.右擊解決方案一>新建項目一>類庫,命名為安裝類(我隨便命名的),右擊安裝類一>添加一>新建類一>安裝程序類,如圖(2.3)

      

                            圖(2.3)

      10.雙擊安裝類一>單擊此處切換到代碼視圖,如圖(2.4)

      

                          如圖(2.4)

      11.重寫安裝類,源碼如下

    

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data.SqlClient;
using System.IO;
using System.Linq;


namespace 安裝類
{
    [RunInstaller(true)]
    public partial class Installer : System.Configuration.Install.Installer
    {
        public Installer()
        {
            InitializeComponent();
}
/// <summary> /// 重寫安裝方法 /// </summary> /// <param name="stateSaver"></param> public override void Install(IDictionary stateSaver) { base.Install(stateSaver); string Server = Context.Parameters["server"].ToString(); string dbName = Context.Parameters["dbname"].ToString(); string userName = Context.Parameters["user"].ToString(); string userPass = Context.Parameters["pwd"].ToString(); string targetdir = Context.Parameters["targetdir"].ToString(); /* * 設置webconfig連接字符串 */ string webconfigpath = Path.Combine(this.Context.Parameters["targetdir"].ToString(), "web.config"); //修改第一個數據庫連接 string webcofnigstring2 = File.ReadAllText(webconfigpath).Replace(@"server=JEFFREY9061\SQL2008;database=yd_esms;uid=sa;pwd=********", GetConnectionString2()); File.WriteAllText(webconfigpath, webcofnigstring2); //修改第二個數據連接 string webcofnigstring = File.ReadAllText(webconfigpath).Replace(@"Data Source=JEFFREY9061\SQL2008;Initial Catalog=yd_esms;Persist Security Info=True;User ID=sa;Password=******", GetConnectionString()); File.WriteAllText(webconfigpath, webcofnigstring); //這個是測試在安裝目錄下添加接收到的用戶填寫的數據庫信息 File.WriteAllText(Path.Combine(targetdir, "log.txt"), Server + "/n/r" + dbName + "/n/r" + userName + "/n/r" + userPass); } /// <summary> /// 執行sql語句 /// </summary> /// <param name="connection"></param> /// <param name="sql"></param> void ExecuteSQL(SqlConnection connection, string sql) { SqlCommand cmd = new SqlCommand(sql, connection); cmd.ExecuteNonQuery(); } /// <summary> /// 獲取文本框輸入的信息,來插入到登錄連接字符串 /// </summary> /// <returns></returns> private string GetConnectionString() { return @"Data Source=" + this.Context.Parameters["server"] + ";Initial Catalog=" + this.Context.Parameters["dbname"] + ";Persist Security Info=True;User ID=" + this.Context.Parameters["user"] + ";Password=" + this.Context.Parameters["pwd"] + ""; } private string GetConnectionString2() { return @"server=" + this.Context.Parameters["server"] + ";database=" + this.Context.Parameters["dbname"] + ";uid=" + this.Context.Parameters["user"] + ";pwd=" + this.Context.Parameters["pwd"]; } } }

 

    12.安裝類點擊生成,又擊WEB安裝包一>添加一>項目輸出,選中安裝類為主輸出,如圖(2.5)

    

                      如圖(2.5)

    13.有擊WEB安裝包一>視圖一>自定義操作,右擊安裝一>添加自定義操作一>雙擊Web應用程序文件夾,選中主輸出來自安裝類(活動),如圖(2.6)

              

                        如圖(2.6)

      14.依次對WEB程序、安裝類、安裝包重新生成

      15.右擊安裝包一>打開資源管理文件夾一>debug,在debug文件夾中會存在exe、msi兩個文件,如圖(2.7)

      

                        如圖(2.7)

     16.雙擊setup.exe 安裝步驟如下

      

 

      

 

 

      

 

 

      

 

 

      

 

 

      

 

 

      

     17.基本上再這里就算結束了,后面的我就不用演示了。

下面講解如何把SQLSERVER數據庫和程序一鍵打包,打包數據庫其實很簡單

      分離數據庫,找到對應的ldf、mdf文件進行復制,粘貼到任意盤,我是放到桌面了,如圖(3.1)

              

                    圖(3.1)

    

      右擊安裝包一>添加一>文件,把ldf、mdf文件添加進去,如圖(3.2)

              、  

                圖(3.2)

      打開安裝類,添加一段代碼,結合WEB源碼如下 

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data.SqlClient;
using System.IO;
using System.Linq;


namespace 安裝類
{
    [RunInstaller(true)]
    public partial class Installer : System.Configuration.Install.Installer
    {
        public Installer()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 重寫安裝方法
        /// </summary>
        /// <param name="stateSaver"></param>
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            string Server = Context.Parameters["server"].ToString();
            string dbName = Context.Parameters["dbname"].ToString();
            string userName = Context.Parameters["user"].ToString();
            string userPass = Context.Parameters["pwd"].ToString();
            string targetdir = Context.Parameters["targetdir"].ToString();

            /*
            * 設置webconfig連接字符串
            */
            string webconfigpath = Path.Combine(this.Context.Parameters["targetdir"].ToString(), "web.config");
            //修改第一個數據庫連接
            string webcofnigstring2 = File.ReadAllText(webconfigpath).Replace(@"server=JEFFREY9061\SQL2008;database=yd_esms;uid=sa;pwd=********", GetConnectionString2());
            File.WriteAllText(webconfigpath, webcofnigstring2);
            //修改第二個數據連接
            string webcofnigstring = File.ReadAllText(webconfigpath).Replace(@"Data Source=JEFFREY9061\SQL2008;Initial Catalog=yd_esms;Persist Security Info=True;User ID=sa;Password=******", GetConnectionString());
            File.WriteAllText(webconfigpath, webcofnigstring);
            //這個是測試在安裝目錄下添加接收到的用戶填寫的數據庫信息
            File.WriteAllText(Path.Combine(targetdir, "log.txt"), Server + "/n/r" + dbName + "/n/r" + userName + "/n/r" + userPass);
            #region 數據庫處理
            string strSql = "";
            if (userPass == "")            
            {
                strSql = "server=" + Server + ";database=master;Integrated Security=True";//連接數據庫字符串      
            }           
            else             
            {
                strSql = "server=" + Server + ";uid=" + userName + ";pwd=" + userPass + ";database=master";//連接數據庫字符串                
            }            
            string DataName = "TEST";//數據庫名          
            string strMdf = targetdir + @"TEST.mdf";//MDF文件路徑,這里需注意文件名要與剛添加的數據庫文件名一樣!      
            string strLdf = targetdir + @"TEST_log.ldf";//LDF文件路徑       
            base.Install(stateSaver);
            this.CreateDataBase(strSql, DataName, strMdf, strLdf, targetdir);//開始創建數據庫 
            #endregion
        }
        /// <summary>
        /// 執行sql語句
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="sql"></param>
        void ExecuteSQL(SqlConnection connection, string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.ExecuteNonQuery();
        }
        /// <summary>
        /// 獲取文本框輸入的信息,來插入到登錄連接字符串
        /// </summary>        
        /// <returns></returns>
        private string GetConnectionString()
        {
            return @"Data Source=" + this.Context.Parameters["server"] + ";Initial Catalog=" + this.Context.Parameters["dbname"] + ";Persist Security Info=True;User ID=" + this.Context.Parameters["user"] + ";Password=" + this.Context.Parameters["pwd"] + "";
        }
        private string GetConnectionString2()
        {
            return @"server=" + this.Context.Parameters["server"] + ";database=" + this.Context.Parameters["dbname"] + ";uid=" + this.Context.Parameters["user"] + ";pwd=" + this.Context.Parameters["pwd"];
        }

        /// <summary>   
        /// 附加數據庫方法        
        /// </summary>          
        /// <param name="strSql">連接數據庫字符串,連接master系統數據庫</param>        
        /// <param name="DataName">數據庫名字</param>          
        /// <param name="strMdf">數據庫文件MDF的路徑</param>        
        /// <param name="strLdf">數據庫文件LDF的路徑</param>        
        /// <param name="path">安裝目錄</param>         
         private void CreateDataBase(string strSql, string DataName, string strMdf, string strLdf, string path)      
         {             
             SqlConnection myConn = new SqlConnection(strSql);         
             String str = null;            
             try
             {
                 str = " EXEC sp_attach_db @dbname='" + DataName + "',@filename1='" + strMdf + "',@filename2='" + strLdf +
                       "'";
                 SqlCommand myCommand = new SqlCommand(str, myConn);
                 myConn.Open();
                 myCommand.ExecuteNonQuery();
                 //MessageBox.Show("數據庫安裝成功!點擊確定繼續");
                 //需Using System.Windows.Forms           
             }
             catch (Exception ex)
             {
                 Console.Write(ex.StackTrace.ToString());
                 //MessageBox.Show("數據庫安裝失敗!" + e.Message + "\n\n" + "您可以手動附加數據");  
                 System.Diagnostics.Process.Start(path);
                 //打開安裝目錄           
             }
             finally
             {
                 myConn.Close();
             }        
         }
    }
}

 

  最后把解決方案全部重新生成,再次重復上面的WEB打包、安裝就好了。。。

  由於空余的時間不是很多,寫的不是很詳細,如有不懂的,可以來我QQ群一起討論

 QQ群號: 8017417   身份認證:Rach技術討論

 如有轉載,請保留原有地址:http://www.cnblogs.com/hank-hu/p/3967101.html 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM