| 发表于:2007-03-11 10:44:0610楼 得分:0 |
using system; using system.io; using system.xml; using system.collections; using system.diagnostics; using system.componentmodel; using system.configuration.install; namespace petshopsetlib { [runinstaller(true)] public class petshopinstaller : system.configuration.install.installer { private system.componentmodel.container components = null; public petshopinstaller() { initializecomponent(); } protected override void dispose( bool disposing ) { if( disposing ) { if(components != null) { components.dispose(); } } base.dispose( disposing ); } public override void install(idictionary statesaver) { base.install (statesaver); // osql -u sa -p sa -q "sp_attach_db 'petshop ', 'e:\petshop\petshop.mdf ' , 'e:\petshop\petshop_log.ldf ' string format = string.format( "-u sa -p sa -q\ "sp_attach_db \ 'petshop\ ',\ '{0}\\petshop.mdf\ ',\ '{1}\\petshop_log.ldf\ ' ", this.context.parameters[ "targetdir "],this.context.parameters[ "targetdir "]); EXECuteosql(format); } public override void uninstall(idictionary savedstate) { try { // osql -u sa -p sa -q "sp_detach_db 'petshop ' string format = "-u sa -p sa -q\ "sp_detach_db \ 'petshop\ ' "; EXECuteosql(format); } catch(exception ex){console.writeline(ex.message);} base.uninstall (savedstate); } // 执行安装和卸载数据库 private void EXECuteosql(string format) { process sqlprocess = new process(); sqlprocess.startinfo.filename = "osql.exe "; sqlprocess.startinfo.arguments = format; sqlprocess.startinfo.windowstyle = processwindowstyle.hidden; sqlprocess.start(); sqlprocess.waitforexit(); sqlprocess.close(); } // 更改配置文件 private void changewebconfig(string pwd) { bool isfound = false; try { fileinfo fileinfo = new fileinfo(this.context.parameters[ "targetdir "] + "\\web.config "); if (! fileinfo.exists) throw new installexception( "没有找到配置文件 "); // 实例化文档 xmldocument xmldoc = new xmldocument(); xmldoc.load(fileinfo.fullname); // 查找appsettings中的节点 foreach (xmlnode node in xmldoc[ "configuration "][ "appsettings "]) { if ((node.name == "add ") && (node.attributes.getnameditem( "key ").value == "connectionstring ")) { // 写入连接字符串 node.attributes.getnameditem( "value ").value = null; //string.format(); isfound = true; break; } } if (!isfound) throw new installexception( "web.config 文件没有包含connectionstring连接字符串设置 "); xmldoc.save(fileinfo.fullname); } catch(exception ex) { throw ex; } } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void initializecomponent() { components = new system.componentmodel.container(); } #endregion } } | | |
|