2012年3月13日 星期二

自動附加 sql db


執行存儲過程sp_attach_db。如:
EXEC sp_attach_db @dbname = N'pubs',
    @filename1 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf',
    @filename2 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs_log.ldf' ;
在SqlServer2005版本及其它更加新的版本,建議用Create Database .... For Attach。




1、創建安裝項目「Setup」安裝項目

在「文件」菜單上指向「添加項目」,然後選擇「新建項目」。

在「添加新項目」對話框中,選擇「項目類型」窗格中的「安裝和部署項目」,然後選擇「模板」窗格中的「安裝項目」。在「名稱」框中鍵入 「setup」(其他名稱也行,隨個人意願)。

單擊「確定」關閉對話框。

項目被添加到解決方案資源管理器中,並且文件系統編輯器打開。

在「屬性」窗口中,選擇 ProductName 屬性,並鍵入」xxxx系統」(你所做項目程序的名稱)。



2、在安裝項目中創建安裝程序類(install.cs)。

在你項目的解決方案上右鍵,選擇「添加」中的「新建項目」。然後選擇「類庫」,在名稱中輸入DbSetUp,位置選擇你的項目程序所在目錄。點「確定」關閉對話框。

在新項目「DbSetUp」上右鍵,選擇「添加」中的「新建項」。然後選擇Visual c#項目項中的「安裝程序類」,點「添加」關閉對話框。

切換到代碼視圖,在install.cs文件中寫入如下代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.Data.SqlClient;

namespace DBSetUp
{
    [RunInstaller(true)]
    public partial class Installer1 : Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }

        private void CreateDataBase()
        {
            // 啟動SQL服務, 預防裝完之後服務未啟動
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";

            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;

            p.Start();
            p.StandardInput.WriteLine("net start MSSQL$Rybx");//Rybx為客戶端數據庫實例名
            p.StandardInput.WriteLine("exit");
            p.StandardOutput.ReadToEnd();

            // 「server」:客戶端數據庫實例名, 「user id」:Sa ,「password」:Sa的密碼
            string strSql = string.Format("server={0}; user id={1}; password={2}; Database=master", @"(local)/Rybx", "sa", "qjs");
            //需要附加的數據庫文件,我的設置是在項目程序的安裝目錄下的database文件夾下
            string strMdf = this.Context.Parameters["targetdir"] + @"/database/RybxMis.mdf";
            string strLdf = this.Context.Parameters["targetdir"] + @"/database/RybxMis.ldf";

            string str;
            SqlConnection myConn = new SqlConnection(strSql);
            str = "EXEC sp_attach_db @dbname = N'RybxMis', @filename1 = N'" + strMdf + "',@filename2=N'" + strLdf + "'";
            SqlCommand myCommand = new SqlCommand(str, myConn);
            myConn.Open();
            myCommand.ExecuteNonQuery();
            myConn.Close();
        }

        protected override void OnAfterInstall(System.Collections.IDictionary savedState)
        {
            base.OnAfterInstall(savedState);
            try
            {
                CreateDataBase();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

    }
}

     單擊「生成」菜單下「生成解決方案」,生成install.dll安裝類文件。

3、將「主程序」項目的輸出添加到部署項目中

     在你項目程序的安裝項目(上面第一步創建的Setup)上右鍵,選擇「視圖」中的「文件系統」,在 「應用程序文件夾」上右鍵,選擇「添加」中的「項目輸出」,打開「添加項目輸出組」對話框,在「項目」下拉表框中選擇你的主安裝程序類,如上面的「DbSetUp」。

     從列表框中選擇「主輸出」組,然後單擊「確定」關閉。

4、創建自定義安裝對話框

如需要可在解決方案資源管理器中選擇安裝項目「Setup」項目,在「視圖」菜單上指向「編輯器」,然後選擇「用戶界面」。在用戶界面編輯器具中,選擇「安裝」下的「啟動」節點。在「操作」菜單上,選擇「添加對話框」。詳細設置請參考msdn,這裡不再詳細說明。

5、建自定義操作

在安裝項目「Setup」項目上右鍵,選擇「視圖」中的「自定義操作」。

在「自定義操作」中選擇「安裝」節點。單擊右鍵「添加自定義操作」,在選擇項目中的項中選擇「應用程序文件夾」,選擇「主輸出來自DbSetUp(活動)」。

左鍵點擊「主輸出來自DbSetUp(活動)」,在「屬性窗口」中選擇「CustomActionData」屬性並鍵入/targetdir="[TARGETDIR]/"。

/targetdir="[TARGETDIR]/" 是你項目程序的安裝路徑,設置此參數以便在install類中獲得該路徑。

  在安裝項目「Setup」項目上右鍵,單擊「生成」,即可生成項目程序的安裝程序,即SetUp.exe。在實際部署中最後需要用Orca MSI安裝文件修改器修改設置SetUp.exe文件,詳細請參照「將MSDE Sp3(注意是sp3)打包進.Net安裝項目中(WinForm應用)」。

沒有留言:

張貼留言