visual stutio 2005 打包部署(三)

 using System;

using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Configuration.Install;
using  System.Data.SqlClient;
using  Microsoft.Win32;
 
namespace  DBAction
{
    [RunInstaller(true)]
    public partial class MyInstaller : Installer
    {
        public MyInstaller()
        {
            InitializeComponent();
        }
 
        private void ExecuteSql(string connString, string DBName, string sqlString)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand comm = new SqlCommand(sqlString, conn);
 
            try
            {
                conn.Open();
                conn.ChangeDatabase(DBName);// 更改当前数据库
                comm.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }
       
        // 重写 Install 方法
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            try
            {
                // 创建数据库
                string connStr = string.Format("data source={0}; user id={1}; password={2}; persist security info = false; packet size=4096",
                    this.Context.Parameters["server"], this.Context.Parameters["User"],
                    this.Context.Parameters["pwd"]);
 
                ExecuteSql(connStr, "master""create database " + this.Context.Parameters["dbname"]);
 
                // 创建一个进程用来执行 sql 脚本 --- 建表
                System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();
 
                // 设置该进程要启动的文档是 "osql.exe" ,这个执行文件用来在命令行执行 sql 脚本文件
                sqlProcess.StartInfo.FileName = "osql.exe";
 
                // 配置进程需要的参数
                //Context.Parameters-- 获取在运行 InstallUtil.exe 时输入的命令行参数
                sqlProcess.StartInfo.Arguments = string.Format("-U {0} -P {1} -i {2}bankDB.sql",
                    this.Context.Parameters["user"], this.Context.Parameters["pwd"],
                    this.Context.Parameters["targetdir"]);
 
                sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
        }
 
        ///   <summary>
        ///    重载 OnBeforeInstall, 确定是否安装 .NETFramework
        ///   </summary>
        protected override void OnBeforeInstall(System.Collections.IDictionary savedState)
        {
            base.OnBeforeInstall(savedState);
            try
            {
                Microsoft.Win32.RegistryKey key;// 定义注册表键
                // 读取相同位置上的信息,若 key 为空,则不存在此键值,则进行安装;相反为存在则不安装
                key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\.NETFramework"true);
                if (key == null)
                {
                    Process.Start(Context.Parameters["targetdir"].ToString() + @" dotnetfx \dotnetfx.exe");// 得到安装后文件的路径,并通过路径和文件名来启动
                }
            }
            catch (Exception e)// 抓取错误信息,并给予提示
            {
                MessageBox.Show(" 安装程序错误!错误提示: "  + e.Message);
            }
        }
    }
}

你可能感兴趣的:(部署,职场,打包,休闲)