.net打包自动安装数据库-.NET教程,Asp.Net研发
来源:作者: 发布时间:2007-12-25 13:34:54

 

一).创建部署项目
1. 在“文档”菜单上指向“添加项目”,然后选择“新建项目”。
2. 在“添加新项目”对话框中,选择“项目类型”窗格中的“安装和部署项目”,然后选择“模板”窗格中的“安装项目”。在“名称”框中键入 setup1。
3. 单击“确定”关闭对话框。
4. 项目被添加到解决方案资源管理器中,并且文档系统编辑器打开。
5. 在“属性”窗口中,选择 productname 属性,并键入 信息管理系统 。
 
二).将 主程式 项目的输出添加到部署项目中
1. 在“文档系统编辑器”中,选择“应用程式文档夹”。在“操作”菜单上,指向“添加”,然后选择“项目输出”。
2. 在“添加项目输出组”对话框中,选择“项目”下拉列表中的“您的程式”。
3. 单击“确定”关闭对话框。
4. 从列表中选择“主输出”和“内容文档”组,然后单击“确定”。
 
三).创建安装程式类
1. 在“文档”菜单上指向“新建”,然后选择“项目”。
2. 在“新建项目”对话框中,选择“项目类型”窗格中的“visual basic 项目”,然后选择“模板”窗格中的“类库”。在“名称”框中键入 installdb。
3. 单击“打开”关闭对话框。
4. 从“项目”菜单中选择“添加新项”。
5. 在“添加新项”对话框中选择“安装程式类”。在“名称”框中键入 installdb。
6. 单击“确定”关闭对话框。
7. 周详代码附后。
 
四).创建自定义安装对话框
1. 在解决方案资源管理器中选择“setup1”项目。在“视图”菜单上指向“编辑器”,然后选择“用户界面”。
2. 在用户界面编辑器中,选择“安装”下的“启动”节点。在“操作”菜单上,选择“添加对话框”。
3. 在“添加对话框”对话框中,选择“许可协议”对话框,然后单击“确定”关闭对话框。
4. 在“添加对话框”对话框中,选择“文本框 (a)”对话框,然后单击“确定”关闭对话框。
5. 在“操作”菜单上,选择“上移”。重复此步骤,直到“文本框 (a)”对话框位于“安装文档夹”节点之上。
6. 在“属性”窗口中,选择 bannertext 属性并键入:安装数据库.
7. 选择 bodytext 属性并键入:安装程式将在目标机器上安装数据库
8. 选择 edit1label 属性并键入:数据库名称:
9. 选择 edit1property 属性并键入 customtexta1
10. 选择 edit1value 属性并键入:dbservers
11. 选择 edit2label 属性并键入:服务器名:
12. 选择 edit2property 属性并键入 customtexta2
13. 选择 edit2value 属性并键入:(local)
14. 选择 edit3label 属性并键入:用户名:
15. 选择 edit3value 属性并键入:sa
16. 选择 edit3property 属性并键入 customtexta3
17. 选择 edit4label 属性并键入:密码:
18. 选择 edit4property 属性并键入 customtexta4
19. 选择 edit2visible、edit3visible 和 edit4visible 属性,并将他们配置为 true
 
五).创建自定义操作
1. 在解决方案资源管理器中选择“setup1”项目。在“视图”菜单上指向“编辑器”,然后选择“自定义操作”。
2. 在自定义操作编辑器中选择“安装”节点。在“操作”菜单上,选择“添加自定义操作”。
3. 在“选择项目中的项”对话框中,双击“应用程式文档夹”。
4. 选择“主输出来自 installdb(活动)”项,然后单击“确定”关闭对话框。
5. 在“属性”窗口中,选择 customactiondata 属性并键入“/dbname=[customtexta1] /server=[customtexta2] /user=[customtexta3] /pwd=[customtexta4] /targetdir="[targetdir]\"”。
 
附:/targetdir="[targetdir]\"是安装后的目标路径,为了在installdb类中获得安装后的路径,我们配置此参数。
六).打包?加入卸载功能:
方法一:
1.在打包?目中添加文档msiexec.exe(一般可在c:\windows\system32\下找到)
2.在文档系???中???用程式文档?,在msiexec.exe上按右?,???建快捷方式,重命名快捷方式?"卸载".
3.更改此快捷方式的arguments 为"/x {?品id}",?品id的值?打包?目的productcode?性值.
方法二:(推荐)
1.先生成安装包,记下productcode(选择解决方案资源管理器根目录如setup1,再查看属性标签,不是右键中的属性),下面要用到
2.用vs.net建立一个新的控制台程式uninst.exe文档
power by: landlordh
for 2000,xp,2003
module uninstall
    sub main()
        dim myprocess as process = new process
        if system.environment.osversion.tostring.indexof("nt 5") then
            myprocess.start("msiexec", "/x{2b65d4a9-c146-4808-ab4b-321fb0779559}")  改为自己的productcode
        end if
        myprocess.close()
    end sub
end module
3.将控制台程式bin目录的exe文档加入到打包程式文档中,在程式组创建uninst.exe的快捷方式
installdb.vb类,要添加引用 system.configuration.install.dll :

using system;
using system.collections;
using system.componentmodel;
using system.configuration.install;
using system.reflection;
using system.io;
using system.data;
using system.data.sqlclient;
namespace install
{
 ///


 /// installer1 的摘要说明。
 ///

 [runinstaller(true)]
 public class installer1 : system.configuration.install.installer
 {
  ///
  /// 必需的设计器变量。
  ///

  private system.componentmodel.container components = null;
  public installer1()
  {
   // 该调用是设计器所必需的。
   initializecomponent();
   // todo: 在 initializecomponent 调用后添加任何初始化
  }
  ///
  /// 清理任何正在使用的资源。
  ///

  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }

  #region 组件设计器生成的代码
  ///


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///

  private void initializecomponent()
  {
   components = new system.componentmodel.container();
  }
  #endregion
  private  string getsql(string name) 
  { 
//   //调用osql执行脚本
//
//   system.diagnostics.process sqlprocess = new system.diagnostics.process();
//
//   sqlprocess.startinfo.filename = "osql.exe";
//
//   sqlprocess.startinfo.arguments = string.format(" -u {0} -p {1} -d {2} -i {3}db.sql", this.context.parameters["user"], this.context.parameters["pwd"],"master", this.context.parameters["targetdir"]);
//
//   sqlprocess.startinfo.windowstyle = system.diagnostics.processwindowstyle.hidden;
//
//   sqlprocess.start();
//
//   sqlprocess.waitforexit() ;//等待执行
//
//   sqlprocess.close();
   try 
   { 
//    assembly asm = assembly.getexecutingassembly();
//    system.io.fileinfo fileinfo = new system.io.fileinfo(asm.location);
//    string path=fileinfo.directoryname+@"\"+name;
    string path=this.context.parameters["targetdir"]+name;
    filestream fs=new filestream(path,filemode.open,fileaccess.read,fileshare.read);
    streamreader reader = new streamreader(fs,system.text.encoding.default); 
    //system.text.encoding.ascii;
    return reader.readtoend(); 
   } 
   catch (exception ex) 
   { 
    console.write("in getsql:"+ex.message); 
    throw ex; 
   } 
  } 
  private void executesql(string databasename,string sql) 
  {
   sqlconnection sqlconnection1=new sqlconnection();
   sqlconnection1.connectionstring =string.format("server={0}; user id={1}; password={2}; database=master",this.context.parameters["server"],this.context.parameters["user"],this.context.parameters["pwd"]);
   system.data.sqlclient.sqlcommand command = new system.data.sqlclient.sqlcommand(sql,sqlconnection1); 
   try 
   {
    command.connection.open(); 
    command.connection.changedatabase(databasename); 
   
    command.executenonquery(); 
   } 
   catch(exception ex) 
   { 
    console.write("in exception handler :"+ex.message); 
   }
   finally 
   { 
    command.connection.close(); 
   } 
  } 

  protected void adddbtable(string strdbname) 
  { 
   try 
   {     
    executesql("master","create database "+ strdbname);
    executesql(strdbname,getsql("sql.txt")); 
    executesql("master","exec sp_addlogin myoamaster,myoamaster,"+strdbname+",null,null");
    executesql(strdbname,"exec sp_grantdbaccess myoamaster, myoamaster");
    executesql(strdbname,"exec sp_addrolemember db_owner,myoamaster");
   } 
   catch(exception ex) 
   { 
    console.write("in exception handler :"+ex.message); 
   } 
  } 
  public override void install(system.collections.idictionary statesaver) 
  { 
   base.install(statesaver); 
   adddbtable(this.context.parameters["dbname"]); 
  } 
 }
}
这里有个sql.txt是数据库的sql脚本,当然能够调用osql来执行sql脚本,其实是相同的。
打包的时候必须把sql.txt文档加进来,否则不会执行。
假如您想附加数据库的mdf文档和ldf文档,用下面这段程式:
private void createdatabase(string strsql,string dataname,string strmdf,string strldf)
  {
   string str;
   sqlconnection myconn = new sqlconnection (strsql);
   //exec sp_detach_db @dbname = bx_freightmileage_2//需要先将数据库分离出来
   str = "exec sp_attach_db @dbname = "+ dataname +", @filename1 = "+ strmdf +",@filename2="+strldf+"";
   sqlcommand mycommand = new sqlcommand(str, myconn);
  
    myconn.open();
    mycommand.executenonquery();
    myconn.close();
  
  }
当然打包的时候也要把这两个数据库文档也加进来。

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅
    Jordan Shoes christian audigier wholesale Nike air Jordan Jordan retro shoes wholesale Nike Shoes Wholesale Shoes Wholesale clothing