我们的解决方案是单独打包,数据库和应用程序分离,数据库可以用一个Installer类来完成,具体操作我就不细说了,网上很多的。

解决方案 »

  1.   

    请问thfthf2() ,我也是用Installer类来打包数据库部分的,并且数据库安装也成功了。但我在系统开始菜单栏里点击快捷方式时,它仍然提示我是否安装数据库,这是我得部分源码
     public override void Install(IDictionary stateSaver)
            {            if (this.Context.Parameters["STRUSER"] != null)
                    this.strUser = this.Context.Parameters["STRUSER"];
                if (this.Context.Parameters["STRPASS"] != null)
                    this.strPass = this.Context.Parameters["STRPASS"];
                if (this.Context.Parameters["STRSERVER"] != null)
                    this.strServer = this.Context.Parameters["STRSERVER"];
                if (this.Context.Parameters["STRDBNAME"] != null)
                    this.strDbName = this.Context.Parameters["STRDBNAME"];
                DialogResult dialogResult = MessageBox.Show("是否在本次安装过程中安装数据库?" , "安装向导", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    AddDBTable();
                }
                try
                {
                    //删除数据库脚本
                    Directory.Delete(this.Context.Parameters["TARGETDIR"] + "\\" + "InstallSql", true);
                }
                catch { }            //写入数据库配置
                ChangeFileToEnableWrite(this.Context.Parameters["TARGETDIR"] +  "UUMTool.exe.config");
                string connectionString = "server=" + strServer + ";database=" + strDbName + ";user id=" + strUser + ";password=" + strPass + ";";
                Encrypt encrypt = new Encrypt();
                connectionString = encrypt.EncryptOrDecrypt(true, connectionString);
                try
                {
                    WriteConnectionInfo(connectionString, this.Context.Parameters["TARGETDIR"] + "UUMTool.exe");
                }
                catch (Exception err)
                {
                    MessageBox.Show("数据库配置写入失败!", "安装向导", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    private void ChangeFileToEnableWrite(string filePath)
            {
                FileInfo fileInfo = new FileInfo(filePath);
                if (fileInfo.Exists)
                {
                    if (fileInfo.IsReadOnly)
                    {
                        File.SetAttributes(filePath, FileAttributes.Normal);
                    }
                }
            }
      

  2.   

    protected void AddDBTable()
            {
                try
                {
                    //Create the database.
                    ExecuteSql("master", "EXEC sp_addextendedproc 'xp_md5', '" + this.Context.Parameters["TARGETDIR"] + "xp_md5.dll'");
                }
                catch (Exception ex)
                {
                    if (!ErrorIsContinue(ex))
                    {
                        throw ex;
                    }
                }
                try
                {
                    ExecuteSql("master", "CREATE DATABASE " + strDbName);
                    // Create the tables.
                }
                catch (Exception ex)
                {
                    if (!ErrorIsContinue(ex))
                    {
                        throw ex;
                    }
                }
                 try
                {
                ExecuteSql(strDbName, GetSql("DBScript.sql"));
                }
                catch (Exception ex)
                {
                    if (!ErrorIsContinue(ex))
                    {
                        throw ex;
                    }
                }
                try
                {
                ExecuteSql(strDbName, GetSql("fn_md5.sql"));
               }
                catch (Exception ex)
                {
                    if (!ErrorIsContinue(ex))
                    {
                        throw ex;
                    }
                } 
                    try
                {
                ExecuteSql(strDbName, GetSql("Get_BMSInfo.sql"));
                }
                catch (Exception ex)
                {
                    if (!ErrorIsContinue(ex))
                    {
                        throw ex;
                    }
                }    
                try
                {
                ExecuteSql(strDbName, GetSql("Get_Password.sql"));
                }
                catch (Exception ex)
                {
                    if (!ErrorIsContinue(ex))
                    {
                        throw ex;
                    }
                }    
                try
                {
                ExecuteSql(strDbName, GetSql("getglobalusername.sql"));
                }
                catch (Exception ex)
                {
                    if (!ErrorIsContinue(ex))
                    {
                        throw ex;
                    }
                }   
                try
                {
                ExecuteSql(strDbName, GetSql("ICPC_SSO_MD5Test.sql"));                                                                                                                                      
                }
                catch (Exception ex)
                {
                    if (!ErrorIsContinue(ex))
                    {
                        throw ex;
                    }
                }  
                    try
                {
                ExecuteSql(strDbName, GetSql("SS_UserGet.sql"));
                }
                catch (Exception ex)
                {
                    if (!ErrorIsContinue(ex))
                    {
                        throw ex;
                    }
                } 
                try
                {
                ExecuteSql(strDbName, GetSql("SS_UserList.sql"));
                }
                catch (Exception ex)
                {
                    if (!ErrorIsContinue(ex))
                    {
                        throw ex;
                    }
                } 
                try
                {
                ExecuteSql(strDbName, GetSql("SS_UserUpdate.sql"));
                }
                catch (Exception ex)
                {
                    if (!ErrorIsContinue(ex))
                    {
                        throw ex;
                    }
                } 
            }
      

  3.   

    /// <summary>
            /// 安装过程中出错是否继续
            /// </summary>
            /// <param name="ex"></param>
            /// <returns></returns>
            private bool ErrorIsContinue(Exception ex)
            {
                DialogResult dialogResult=MessageBox.Show("安装过程中,发生以下错误,是否继续安装?" + System.Environment.NewLine + ex.Message, "安装向导", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    return true;
                }
                return false;
            }
            private void ExecuteSql(string DatabaseName, string Sql)
            {
                SqlConnection sqlConnection1 = new SqlConnection("user id=" + strUser + ";password=" + strPass + ";database=master;server=" + strServer);
                SqlCommand Command = new SqlCommand(Sql, sqlConnection1);
                Command.Connection.Open();
                Command.Connection.ChangeDatabase(DatabaseName);
                try
                {
                    Command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    Command.Connection.Close();
                }
            }
            private string GetSql(string strName)
            {
                try
                {
                    string fileNote = File.ReadAllText(this.Context.Parameters["TARGETDIR"] + "\\InstallSql\\" + strName);
                    return fileNote;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
      

  4.   

    一般不用他自己的打包用INSTALLSHILD也不挫吗