各位大仙,知道的帮帮忙吧。或者别的打包工具也可以。需求如下1、自动检测是否已经安装了数据库,没安装则自动安装,并恢复数据库2、自动检测 framework 是否安装,没则进行安装谢谢各位了

解决方案 »

  1.   


    #region 自定义检测当前机器是否安装SQL2000/MSDE方法
    public static bool ExistSqlServerService()
    {
    string strErrorMessage = "";
    try
    {
    //SQL Server2000 SP4的值8.00.194 
    RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true).OpenSubKey(@"Microsoft\MSSQLServer\MSSQLServer\CurrentVersion", true);
    if (regkey == null)
    {
    return false;
    }
    else
    {
    return true;
    } }
    catch (Exception ex)
    {
    strErrorMessage = ex.Message.ToString();
    return false;
    }
    }
    #endregion #region 判断初始数据库是否存在
    public static bool CheckDataBaseExist()
    {
    ComDB db = new ComDB();
    string strSql = "select   *   from sysdatabases   where   [name]   = 'LMS'";
    ComConfig config = new ComConfig();
    string connString = config.connectionSimpleString;
    if (config.connectionSimpleString == "")
    {
    MessageBox.Show("请确定ROKET3.DAT文件是否存在!", "数据库连接", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    return false;
    }
    SqlConnection con = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand(strSql, con);
    DataSet ds = new DataSet();
    try
    {
    con.Open();
    //dt = db.DbDataTable(strSql, "Test");
    SqlDataAdapter command = new SqlDataAdapter(strSql, con);
    command.Fill(ds, "LMS");
    }
    catch (System.Data.SqlClient.SqlException E)
    {
    //throw new Exception(E.Message);
    MessageBox.Show(E.Message);
    return false;
    }
    finally
    {
    con.Close();
    }
    if (ds.Tables[0].Rows.Count > 0)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    #endregion #region 恢复初始数据库 public static bool replayDataBase()
    {
    ComDB db = new ComDB(); //取得数据库文件存放根目录
    string SQLDataRoot = Registry.LocalMachine.OpenSubKey("SOFTWARE", true).OpenSubKey(@"Microsoft\MSSQLServer\Setup", true).GetValue("SQLDataRoot").ToString();
    //备份并删除原始数据库文件 ADD BY UBIQ-LIU 2010/8/16
    string pathOldmdf = SQLDataRoot + @"\Data\LMS_Data.mdf";
    string pathOldldf = SQLDataRoot + @"\Data\LMS_log.ldf";
    //将安装包内的文件拷入SQL DATA根目录中
    string path = System.Windows.Forms.Application.StartupPath;
    string pathNewmdf = path + @"\LMS_Data.mdf";
    string pathNewldf = path + @"\LMS_log.ldf";
    if (File.Exists(pathOldmdf))
    {
    MessageBox.Show("发现已存在的数据库文件,将为您备份。\n之前的数据暂时不可用,请妥善保管您的备份文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    FileInfo filcopy = new FileInfo(pathOldmdf);
    filcopy.CopyTo(pathOldmdf + "_bak" + DateTime.Now.ToString("yyyyMMddhhmmss"));
    File.Delete(pathOldmdf);//备份删除源文件
    }
    //拷贝进新文件
    //将安装包内的文件拷入SQL DATA根目录中
    FileInfo filNewcopy = new FileInfo(pathNewmdf);
    filNewcopy.CopyTo(pathOldmdf);
    if (File.Exists(pathOldldf))
    {
    FileInfo filcopy = new FileInfo(pathOldldf);
    filcopy.CopyTo(pathOldldf + "_bak" + DateTime.Now.ToString("yyyyMMddhhmmss"));
    File.Delete(pathOldldf);//备份删除源文件
    }
    //拷贝进新文件
    //将安装包内的文件拷入SQL DATA根目录中
    filNewcopy = new FileInfo(pathNewldf);
    filNewcopy.CopyTo(pathOldldf);
    //MessageBox.Show(pathNewmdf);
    if (!File.Exists(pathNewmdf))
    {
    MessageBox.Show("未找到数据库备份文件", "数据导入错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    // Close();
    return false;
    }
    //MessageBox.Show(path);//TEST
    //string restore = "restore database LMS from disk='" + path + "';";
    //附加数据库
    string restore = "sp_attach_db 'LMS', '" + pathOldmdf + "','" + pathOldldf + "';";
    ComConfig config = new ComConfig();
    string connString = config.connectionSimpleString;
    SqlConnection con = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand(restore, con);
    try
    {
    con.Open();
    //MessageBox.Show("连接打开成功!");
    cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message, "数据库恢复失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    return false;
    }
    finally
    {
    con.Close();
    }
    return true;
    } #endregion
      

  2.   

    ComDB类,是数据库操作类,自己写一个或者网上找一个,由于代码多,就不贴了,这个是我做过的项目,真实可用。
    2、第二个问题,没有安装framework的话你的程序就运行不起来,谈不上检测framework了,只能用打包的时候,将framework设为必装项目。看这个例子就行
    http://www.cnblogs.com/scottckt/archive/2011/05/14/2046313.html
      

  3.   


    试图安装“C:\Users\Administrator\Documents\Visual Studio 2010\Projects\Setup6\Setup6\Debug\Setup6.msi”时发生以下错误:
    “拒绝访问。 ”
      

  4.   

      #region 恢复初始数据库           public static bool replayDataBase()         {             ComDB db = new ComDB();               //取得数据库文件存放根目录             string SQLDataRoot = Registry.LocalMachine.OpenSubKey("SOFTWARE", true).OpenSubKey(@"Microsoft\MSSQLServer\Setup", true).GetValue("SQLDataRoot").ToString();             //备份并删除原始数据库文件 ADD BY UBIQ-LIU 2010/8/16             string pathOldmdf = SQLDataRoot + @"\Data\LMS_Data.mdf";             string pathOldldf = SQLDataRoot + @"\Data\LMS_log.ldf";             //将安装包内的文件拷入SQL DATA根目录中             string path = System.Windows.Forms.Application.StartupPath;             string pathNewmdf = path + @"\LMS_Data.mdf";             string pathNewldf = path + @"\LMS_log.ldf";             if (File.Exists(pathOldmdf))             {                 MessageBox.Show("发现已存在的数据库文件,将为您备份。\n之前的数据暂时不可用,请妥善保管您的备份文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                 FileInfo filcopy = new FileInfo(pathOldmdf);                 filcopy.CopyTo(pathOldmdf + "_bak" + DateTime.Now.ToString("yyyyMMddhhmmss"));                 File.Delete(pathOldmdf);//备份删除源文件             }             //拷贝进新文件             //将安装包内的文件拷入SQL DATA根目录中             FileInfo filNewcopy = new FileInfo(pathNewmdf);             filNewcopy.CopyTo(pathOldmdf);             if (File.Exists(pathOldldf))             {                 FileInfo filcopy = new FileInfo(pathOldldf);                 filcopy.CopyTo(pathOldldf + "_bak" + DateTime.Now.ToString("yyyyMMddhhmmss"));                 File.Delete(pathOldldf);//备份删除源文件             }             //拷贝进新文件             //将安装包内的文件拷入SQL DATA根目录中             filNewcopy = new FileInfo(pathNewldf);             filNewcopy.CopyTo(pathOldldf);             //MessageBox.Show(pathNewmdf);             if (!File.Exists(pathNewmdf))             {                 MessageBox.Show("未找到数据库备份文件", "数据导入错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);                 // Close();                 return false;             }             //MessageBox.Show(path);//TEST             //string restore = "restore database LMS from disk='" + path + "';";             //附加数据库             string restore = "sp_attach_db 'LMS', '" + pathOldmdf + "','" + pathOldldf + "';";             ComConfig config = new ComConfig();             string connString = config.connectionSimpleString;             SqlConnection con = new SqlConnection(connString);             SqlCommand cmd = new SqlCommand(restore, con);             try            {                 con.Open();                 //MessageBox.Show("连接打开成功!");                 cmd.ExecuteNonQuery();             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message, "数据库恢复失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);                 return false;             }             finally            {                 con.Close();             }             return true;         }           #endregion
      

  5.   


    这个要自己另外做一个WINFOREM界面的?
      

  6.   

    这些方法放在Program.cs
    里面就行啊。
      

  7.   

    都不好好看我给的代码,第一次回答就给了你附加数据库的代码, //附加数据库             string restore = "sp_attach_db 'LMS', '" + pathOldmdf + "','" + pathOldldf + "';";             ComConfig config = new ComConfig();             string connString = config.connectionSimpleString;             SqlConnection con = new SqlConnection(connString);             SqlCommand cmd = new SqlCommand(restore, con);             try            {                 con.Open();                 //MessageBox.Show("连接打开成功!");                 cmd.ExecuteNonQuery();             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message, "数据库恢复失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);                 return false;             }             finally            {                 con.Close();             }             return true;         }   
      

  8.   


    /// <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 e)
       {
          MessageBox.Show("数据库安装失败!" + e.Message+"\n\n"+"您可以手动附加数据");
          System.Diagnostics.Process.Start(path);//打开安装目录
       }
       finally
       {
          myConn.Close();
       }
    }
    public override void Install(System.Collections.IDictionary stateSaver)
    {
       string server = this.Context.Parameters["server"];//服务器名称
        string uid = this.Context.Parameters["user"];//SQlServer用户名
        string pwd = this.Context.Parameters["pwd"];//密码
        string path = this.Context.Parameters["targetdir"];//安装目录
        string strSql = "server=" + server + ";uid=" + uid + ";pwd=" + pwd + ";database=master";//连接数据库字符串
        string DataName = "JXC";//数据库名
        string strMdf = path + @"JXC.mdf";//MDF文件路径,这里需注意文件名要与刚添加的数据库文件名一样!
        string strLdf = path + @"jxc_log.ldf";//LDF文件路径
        base.Install(stateSaver);
       this.CreateDataBase(strSql, DataName, strMdf, strLdf, path);//开始创建数据库