.net打包,包含MSDE数据库

来源:互联网 发布:vip域名后缀可以备案 编辑:程序博客网 时间:2024/04/29 02:03

.net 程序 包含(msde)打包步骤:

 

工具: vs2005, Msde sp3a, Orca

 

1.用vs2005新建 安装项目 setup1

 

2.在 文件系统->应用程序文件夹 添加要打包的目标程序

   

3.在 用户的“程序”菜单和用户桌面添加主程序的快捷方式

 

4.添加 许可协议

用安装项目上右键选择用户界面, 添加对话框 ,在属性选择许可协议文本;

 

5.修改安装项目的属性:

 

6.修改发布属性,

在资料管理器 右键安装项目setup1,选择属性,点击系统必备,勾选dotnetframework 2.0和MADC

 

7.打包MSDE sp3a

  选择“文件系统编辑器”,在“视图“菜单上指向“添加”,然后选择“合并模块…”(Merge Moudle),在添加模块中,找到MSDE sp3安装文件所在目录,将MSMMSM/1033下的所有文件,添加进来。

在安装项目Setup1的属性(Properties)中的“Search Path”,添加MSMMSM/1033MSM/2052目录。

 

生成安装文件 setup1.msi后:

用Orca MSI安装文件修改器打开生成的安装包(Setup1.msi)文件,在左列的表栏中

选择“InstallExecuteSequence”表,修改下面的属性值:

GetSqlStates.XXXXXX 103改成421

RemoveExistingProducts值改成1800

InstallInitialize值改成1799

     在InstallUISequence

选择“InstallUISequence”表,修改下面的值:

GetSqlStates.XXXXXX 103改成421

选择“Property”表,添加以下三个属性:

SqlInstanceName:nhls   实例服务名

SqlSecurityMode:SQL   (不加这行确实也行,就是没办法用SQL模式登录)

SqlSaPwd:nhls  sa的密码

8.附件数据库

  新建一个类库项目InstallAction,删除自动带出的class1.cs,添加新项,选择选择安装程序类InstallClass.cs;

代码如下:

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Configuration.Install;
  5. using System.Data.SqlClient;
  6. using System.Reflection;
  7. using System.Diagnostics;
  8. namespace installAction
  9. {
  10.     [RunInstaller(true)]
  11.     public partial class installClass : Installer
  12.     {
  13.         public installClass()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         //server:nhls 服务器名称
  18.         //user:sa    用户名
  19.         //pwd:nhls    密码
  20.         //重写虚构函数,安装程序中会自动执行这个函数
  21.         public override void Install(System.Collections.IDictionary stateSaver)
  22.         {
  23.             base.Install(stateSaver);
  24.             Assembly Asm = Assembly.GetExecutingAssembly(); 
  25.             System.IO.FileInfo FileInfo = new System.IO.FileInfo(Asm.Location);
  26.             string path = FileInfo.DirectoryName;                                //得到当前安装的程序的路径
  27.             //AddDBTable(this.Context.Parameters["dbname"]);
  28.             string con = "server=localhost//nhls;uid=sa;pwd=nhls;database=master";
  29.             string DataName = "rsgl";
  30.             string strMd = path + @"/" + "rsgl_Data.MDF";
  31.             string strLdf = path + @"/" + "rsgl_Log.LDF"; 
  32.             RunSqlServer();                                                    //启动sql 服务
  33.             CreateDataBase(con, DataName, strMd, strLdf); //附加数据库
  34.         }
  35.         //如果你想附加数据库的mdf文件和ldf文件,用下面这段程序:
  36.         private void CreateDataBase(string strSql, string DataName, string strMdf, string strLdf)
  37.         {
  38.             String str;
  39.             SqlConnection myConn = new SqlConnection(strSql);
  40.             //EXEC sp_detach_db @dbname = 'BX_FreightMileage_2'//需要先将数据库分离出来
  41.             str = "EXEC sp_attach_db @dbname = '" + DataName + "', @filename1 = '" + strMdf + "',@filename2='" + strLdf + "'";
  42.             SqlCommand myCommand = new SqlCommand(str, myConn);
  43.             myConn.Open();
  44.             myCommand.ExecuteNonQuery();
  45.             myConn.Close();
  46.         }
  47.       public void RunSqlServer()
  48.         {
  49.             Process p = new Process();
  50.             p.StartInfo.FileName = "cmd.exe";          //設定程序名
  51.             p.StartInfo.Arguments = "/c /"net start MSSQL$nhls/" "; //設定程式執行參數观察服务的名称就可以知道为什么是MSSQL$ZJJ
  52.             p.StartInfo.UseShellExecute = false;        //關閉Shell的使用
  53.             p.StartInfo.RedirectStandardInput = true;  //
  54.             p.StartInfo.RedirectStandardOutput = true;  //
  55.             p.StartInfo.RedirectStandardError = true;  //重定向错误输出
  56.             p.StartInfo.CreateNoWindow = true;          //设置不显示窗口
  57.             p.Start();  //启动   
  58.         }
  59.         
  60.     }
  61. }

 

编译生成 InstallAction.dll; 在安装项目的应用程序文件夹添加 InstallAction.dll;

 

安装项目-》视图-》自定义操作-》安装  下添加自定义操作,选择应用程序文件夹-InstallAction.dll

 

 

9编译生成 setup.msi, 用Orca 修改参数(参见步骤7)