installshield安装文件的制作小技巧--附加数据库
2007-07-13 15:12
267 查看
installshield安装文件的制作小技巧--附加数据库
最近又一个项目接近尾声,不过对方要求安装必须傻瓜化,尽量不用手动配置。于是就的重新捡起两三年没有用过的installshield了。找到 installshield 12 于是,着手开始制作安装包。因为是网站项目,所以,其他方面倒是问题不大,因为当年用installshield 6.2的时候,也是相当熟练的。最主要的有两个方面:一、数据库的安装,因为我的数据库里面有许多存储过程和初始数据,所以不想直接用script来初始化。于是就想直接在安装包中把数据库文件和日志文件打包,然后在安装时直接附加就行。二、修改web.config文件中的数据库连接字符串和一些相关的参数,比如一些路径相关的参数等等。
其中第一点经过摸索和总结,有两种方法:1、直接使用自定义的函数,声称sql语句,然后调用osql.exe来执行。
函数如下:
function number CreateDataBase(svSQLsvr,svSQLusr,svSQLpwd)
500)this.style.width=500;" align=top>STRING szCmdLine,szWaitTxt;
500)this.style.width=500;" align=top>begin
500)this.style.width=500;" align=top>szWaitTxt=" 正在创建所需数据库500)this.style.width=500;" align=top>SdShowMsg (szWaitTxt, TRUE);
500)this.style.width=500;" align=top>Delay(2);
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top> if(g_bWinLogin) then
500)this.style.width=500;" align=top> szCmdLine = "/E /S "+svSQLsvr+" /Q \"EXEC sp_attach_db @dbname = N'dlbj',@filename1 = N'"+TARGETDIR ^"mydb.MDF',@filename2 = N'"+TARGETDIR ^"dlbj_web_log.LDF'\"";
500)this.style.width=500;" align=top> else
500)this.style.width=500;" align=top> szCmdLine = "/U "+svSQLusr+" /P "+svSQLpwd+" /S "+svSQLsvr+" /Q \"EXEC sp_attach_db @dbname = N'dlbj',@filename1 = N'"+TARGETDIR ^"dlbj_web.MDF',@filename2 = N'"+TARGETDIR ^"mydb_log.LDF'\"";
500)this.style.width=500;" align=top> endif;
500)this.style.width=500;" align=top>//szCmdLine = "/U "+svSQLusr+" /P "+svSQLpwd+" /S "+svSQLsvr+" /Q \"EXEC sp_attach_db @dbname = N'dlbj',@filename1 = N'"+TARGETDIR ^"dlbj_web.MDF',@filename2 = N'"+TARGETDIR ^"dlbmydb_log.LDF'\"";
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top>if (LaunchAppAndWait("osql.exe", szCmdLine,WAIT) < 0) then
500)this.style.width=500;" align=top>MessageBox ("数据库创建失败!请确您的系统中已安装 Microsoft SQL Server 2000. 如仍无法解决,请联系系统供应商!",SEVERE);
500)this.style.width=500;" align=top>endif;
500)this.style.width=500;" align=top>SdShowMsg (szWaitTxt, FALSE);
500)this.style.width=500;" align=top>szWaitTxt=" 正在优化系统数据库500)this.style.width=500;" align=top>SdShowMsg (szWaitTxt, TRUE);
500)this.style.width=500;" align=top>Delay(2);
500)this.style.width=500;" align=top>szCmdLine = "/U "+svSQLusr+" /P "+svSQLpwd+" /S "+svSQLsvr+" /Q \"use dlbj ; exec sp_updatestats\"";
500)this.style.width=500;" align=top>if (LaunchAppAndWait("osql.exe", szCmdLine,WAIT) < 0) then
500)this.style.width=500;" align=top>MessageBox ("数据库优化失败!您可以在 sql查询分析器中执行 use dlbj ; exec sp_updatestats 完成!",SEVERE);
500)this.style.width=500;" align=top>endif;
500)this.style.width=500;" align=top>SdShowMsg (szWaitTxt, FALSE);
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top>return 0;
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top>end;
然后在OnEnd中执行如下代码:
if !MAINTENANCE then
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top>CreateDataBase(g_szServer,g_szUser,g_szPassword);
endif;
其中,g_szServer,g_szUser,g_szPassword分别为,在installshield中数据库设置界面(或者自定义窗口)中操作得到的服务器地址,用户名和密码。
2、直接在 安装设计视图中创建连接后,直接创建一个sqlscript,会在安装的时候直接执行。这样的话就的提前知道安装的位置(这是不可能的),于是就直接写成可替换的字符串,然后使用 text replacement 选项卡,设置替换项。其中,sqlscript如下:
USE master;
500)this.style.width=500;" align=top> GO
500)this.style.width=500;" align=top>-- Drop database if it exists.
500)this.style.width=500;" align=top>IF EXISTS (SELECT name FROM sys.databases WHERE NAME = 'dmydb')
500)this.style.width=500;" align=top> DROP DATABASE dmydb;
500)this.style.width=500;" align=top>GO
500)this.style.width=500;" align=top>CREATE DATABASE dlbj ON PRIMARY
500)this.style.width=500;" align=top> (FILENAME =
500)this.style.width=500;" align=top> '%TEXT_TO_BE_REPLACED_IN_THE_FILE%\dmydb.mdf')
500)this.style.width=500;" align=top> LOG ON (FILENAME =
500)this.style.width=500;" align=top> '%TEXT_TO_BE_REPLACED_IN_THE_FILE%\dmydb_log.LDF')
500)this.style.width=500;" align=top> FOR ATTACH;
500)this.style.width=500;" align=top>GO
其中:%TEXT_TO_BE_REPLACED_IN_THE_FILE% 是要替换为安装目录的字符串。
这里有个值得注意的地方,如果是installscript项目。那么,在text replacement 选项卡,替换后的内容不能直接设置为[INSTALLDIR]或者 [TARGETDIR],而是要同样设置为一个替换变量,如:<USER_DATA>,即就是,把:%TEXT_TO_BE_REPLACED_IN_THE_FILE% 替换为:<USER_DATA>,然后在 installscript 的 Dlg_SdAskDestPath2: 代码段中,执行如下语句即可。
TextSubSetValue ("<USER_DATA>", TARGETDIR , TRUE );
当时这里困扰我很长世界,最后还是上国外的网站上找到了解决方案。
对于第二点,则,直接使用函数:
function UpdateKey( svFilePath, svFindEntry, svNewEntry)
500)this.style.width=500;" align=top> NUMBER nResult, nvLineNumber;
500)this.style.width=500;" align=top> STRING WebConfigFile, svReturnLine;
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top> begin
500)this.style.width=500;" align=top> // build the path to the Web.Config
500)this.style.width=500;" align=top> WebConfigFile = svFilePath ^ "web.config";
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top> // search the file for the key we specify
500)this.style.width=500;" align=top> nResult = FileGrep( WebConfigFile, svFindEntry, svReturnLine, nvLineNumber, RESTART );
500)this.style.width=500;" align=top> switch(nResult)
500)this.style.width=500;" align=top> case 0:
500)this.style.width=500;" align=top> // Since this line normally appears twice in our web.config file, we're
500)this.style.width=500;" align=top> // checking for the existence of a 2nd match and updating it instead.
500)this.style.width=500;" align=top> // If there is no 2nd match, go ahead and update the one we find.
500)this.style.width=500;" align=top> if ( svFindEntry = "connectionString" ) then
500)this.style.width=500;" align=top> nResult = FileGrep( WebConfigFile, svFindEntry , svReturnLine, nvLineNumber, CONTINUE );
500)this.style.width=500;" align=top> if ( nResult < 0 ) then
500)this.style.width=500;" align=top> FileGrep( WebConfigFile, svFindEntry, svReturnLine, nvLineNumber, RESTART );
500)this.style.width=500;" align=top> endif;
500)this.style.width=500;" align=top> endif;
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top> // once we find the key, update (replace) it with our new values
500)this.style.width=500;" align=top> if ( FileInsertLine( WebConfigFile, svNewEntry, nvLineNumber, REPLACE ) < 0 ) then
500)this.style.width=500;" align=top> // hmmm500)this.style.width=500;" align=top> MessageBox( "Unable to update Web.Config file.", SEVERE );
500)this.style.width=500;" align=top> endif;
500)this.style.width=500;" align=top> case -2:
500)this.style.width=500;" align=top> // File Not Found
500)this.style.width=500;" align=top> MessageBox( "Web.Config file not found.", SEVERE );
500)this.style.width=500;" align=top> case -4:
500)this.style.width=500;" align=top> // EOF reached
500)this.style.width=500;" align=top> MessageBox( svFindEntry + " key not found.", SEVERE );
500)this.style.width=500;" align=top> default:
500)this.style.width=500;" align=top> //unknown error
500)this.style.width=500;" align=top> MessageBox( "An unknown error has occurred. The Web.Config file has NOT been updated.", SEVERE );
500)this.style.width=500;" align=top> endswitch;
500)this.style.width=500;" align=top> end;
即可。
本文为转贴,原帖地址为:http://www.cnblogs.com/culturenet/archive/2006/12/19/596930.html
最近又一个项目接近尾声,不过对方要求安装必须傻瓜化,尽量不用手动配置。于是就的重新捡起两三年没有用过的installshield了。找到 installshield 12 于是,着手开始制作安装包。因为是网站项目,所以,其他方面倒是问题不大,因为当年用installshield 6.2的时候,也是相当熟练的。最主要的有两个方面:一、数据库的安装,因为我的数据库里面有许多存储过程和初始数据,所以不想直接用script来初始化。于是就想直接在安装包中把数据库文件和日志文件打包,然后在安装时直接附加就行。二、修改web.config文件中的数据库连接字符串和一些相关的参数,比如一些路径相关的参数等等。
其中第一点经过摸索和总结,有两种方法:1、直接使用自定义的函数,声称sql语句,然后调用osql.exe来执行。
函数如下:
function number CreateDataBase(svSQLsvr,svSQLusr,svSQLpwd)
500)this.style.width=500;" align=top>STRING szCmdLine,szWaitTxt;
500)this.style.width=500;" align=top>begin
500)this.style.width=500;" align=top>szWaitTxt=" 正在创建所需数据库500)this.style.width=500;" align=top>SdShowMsg (szWaitTxt, TRUE);
500)this.style.width=500;" align=top>Delay(2);
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top> if(g_bWinLogin) then
500)this.style.width=500;" align=top> szCmdLine = "/E /S "+svSQLsvr+" /Q \"EXEC sp_attach_db @dbname = N'dlbj',@filename1 = N'"+TARGETDIR ^"mydb.MDF',@filename2 = N'"+TARGETDIR ^"dlbj_web_log.LDF'\"";
500)this.style.width=500;" align=top> else
500)this.style.width=500;" align=top> szCmdLine = "/U "+svSQLusr+" /P "+svSQLpwd+" /S "+svSQLsvr+" /Q \"EXEC sp_attach_db @dbname = N'dlbj',@filename1 = N'"+TARGETDIR ^"dlbj_web.MDF',@filename2 = N'"+TARGETDIR ^"mydb_log.LDF'\"";
500)this.style.width=500;" align=top> endif;
500)this.style.width=500;" align=top>//szCmdLine = "/U "+svSQLusr+" /P "+svSQLpwd+" /S "+svSQLsvr+" /Q \"EXEC sp_attach_db @dbname = N'dlbj',@filename1 = N'"+TARGETDIR ^"dlbj_web.MDF',@filename2 = N'"+TARGETDIR ^"dlbmydb_log.LDF'\"";
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top>if (LaunchAppAndWait("osql.exe", szCmdLine,WAIT) < 0) then
500)this.style.width=500;" align=top>MessageBox ("数据库创建失败!请确您的系统中已安装 Microsoft SQL Server 2000. 如仍无法解决,请联系系统供应商!",SEVERE);
500)this.style.width=500;" align=top>endif;
500)this.style.width=500;" align=top>SdShowMsg (szWaitTxt, FALSE);
500)this.style.width=500;" align=top>szWaitTxt=" 正在优化系统数据库500)this.style.width=500;" align=top>SdShowMsg (szWaitTxt, TRUE);
500)this.style.width=500;" align=top>Delay(2);
500)this.style.width=500;" align=top>szCmdLine = "/U "+svSQLusr+" /P "+svSQLpwd+" /S "+svSQLsvr+" /Q \"use dlbj ; exec sp_updatestats\"";
500)this.style.width=500;" align=top>if (LaunchAppAndWait("osql.exe", szCmdLine,WAIT) < 0) then
500)this.style.width=500;" align=top>MessageBox ("数据库优化失败!您可以在 sql查询分析器中执行 use dlbj ; exec sp_updatestats 完成!",SEVERE);
500)this.style.width=500;" align=top>endif;
500)this.style.width=500;" align=top>SdShowMsg (szWaitTxt, FALSE);
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top>return 0;
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top>end;
然后在OnEnd中执行如下代码:
if !MAINTENANCE then
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top>CreateDataBase(g_szServer,g_szUser,g_szPassword);
endif;
其中,g_szServer,g_szUser,g_szPassword分别为,在installshield中数据库设置界面(或者自定义窗口)中操作得到的服务器地址,用户名和密码。
2、直接在 安装设计视图中创建连接后,直接创建一个sqlscript,会在安装的时候直接执行。这样的话就的提前知道安装的位置(这是不可能的),于是就直接写成可替换的字符串,然后使用 text replacement 选项卡,设置替换项。其中,sqlscript如下:
USE master;
500)this.style.width=500;" align=top> GO
500)this.style.width=500;" align=top>-- Drop database if it exists.
500)this.style.width=500;" align=top>IF EXISTS (SELECT name FROM sys.databases WHERE NAME = 'dmydb')
500)this.style.width=500;" align=top> DROP DATABASE dmydb;
500)this.style.width=500;" align=top>GO
500)this.style.width=500;" align=top>CREATE DATABASE dlbj ON PRIMARY
500)this.style.width=500;" align=top> (FILENAME =
500)this.style.width=500;" align=top> '%TEXT_TO_BE_REPLACED_IN_THE_FILE%\dmydb.mdf')
500)this.style.width=500;" align=top> LOG ON (FILENAME =
500)this.style.width=500;" align=top> '%TEXT_TO_BE_REPLACED_IN_THE_FILE%\dmydb_log.LDF')
500)this.style.width=500;" align=top> FOR ATTACH;
500)this.style.width=500;" align=top>GO
其中:%TEXT_TO_BE_REPLACED_IN_THE_FILE% 是要替换为安装目录的字符串。
这里有个值得注意的地方,如果是installscript项目。那么,在text replacement 选项卡,替换后的内容不能直接设置为[INSTALLDIR]或者 [TARGETDIR],而是要同样设置为一个替换变量,如:<USER_DATA>,即就是,把:%TEXT_TO_BE_REPLACED_IN_THE_FILE% 替换为:<USER_DATA>,然后在 installscript 的 Dlg_SdAskDestPath2: 代码段中,执行如下语句即可。
TextSubSetValue ("<USER_DATA>", TARGETDIR , TRUE );
当时这里困扰我很长世界,最后还是上国外的网站上找到了解决方案。
对于第二点,则,直接使用函数:
function UpdateKey( svFilePath, svFindEntry, svNewEntry)
500)this.style.width=500;" align=top> NUMBER nResult, nvLineNumber;
500)this.style.width=500;" align=top> STRING WebConfigFile, svReturnLine;
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top> begin
500)this.style.width=500;" align=top> // build the path to the Web.Config
500)this.style.width=500;" align=top> WebConfigFile = svFilePath ^ "web.config";
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top> // search the file for the key we specify
500)this.style.width=500;" align=top> nResult = FileGrep( WebConfigFile, svFindEntry, svReturnLine, nvLineNumber, RESTART );
500)this.style.width=500;" align=top> switch(nResult)
500)this.style.width=500;" align=top> case 0:
500)this.style.width=500;" align=top> // Since this line normally appears twice in our web.config file, we're
500)this.style.width=500;" align=top> // checking for the existence of a 2nd match and updating it instead.
500)this.style.width=500;" align=top> // If there is no 2nd match, go ahead and update the one we find.
500)this.style.width=500;" align=top> if ( svFindEntry = "connectionString" ) then
500)this.style.width=500;" align=top> nResult = FileGrep( WebConfigFile, svFindEntry , svReturnLine, nvLineNumber, CONTINUE );
500)this.style.width=500;" align=top> if ( nResult < 0 ) then
500)this.style.width=500;" align=top> FileGrep( WebConfigFile, svFindEntry, svReturnLine, nvLineNumber, RESTART );
500)this.style.width=500;" align=top> endif;
500)this.style.width=500;" align=top> endif;
500)this.style.width=500;" align=top>
500)this.style.width=500;" align=top> // once we find the key, update (replace) it with our new values
500)this.style.width=500;" align=top> if ( FileInsertLine( WebConfigFile, svNewEntry, nvLineNumber, REPLACE ) < 0 ) then
500)this.style.width=500;" align=top> // hmmm500)this.style.width=500;" align=top> MessageBox( "Unable to update Web.Config file.", SEVERE );
500)this.style.width=500;" align=top> endif;
500)this.style.width=500;" align=top> case -2:
500)this.style.width=500;" align=top> // File Not Found
500)this.style.width=500;" align=top> MessageBox( "Web.Config file not found.", SEVERE );
500)this.style.width=500;" align=top> case -4:
500)this.style.width=500;" align=top> // EOF reached
500)this.style.width=500;" align=top> MessageBox( svFindEntry + " key not found.", SEVERE );
500)this.style.width=500;" align=top> default:
500)this.style.width=500;" align=top> //unknown error
500)this.style.width=500;" align=top> MessageBox( "An unknown error has occurred. The Web.Config file has NOT been updated.", SEVERE );
500)this.style.width=500;" align=top> endswitch;
500)this.style.width=500;" align=top> end;
即可。
本文为转贴,原帖地址为:http://www.cnblogs.com/culturenet/archive/2006/12/19/596930.html
相关文章推荐
- installshield安装文件的制作小技巧--附加数据库
- 转:installshield安装文件的制作小技巧--附加数据库
- installshield安装文件的制作小技巧--附加数据库
- installshield安装文件的制作小技巧--附加数据库
- [导入]installshield安装文件的制作小技巧--附加数据库
- 数据库安装文件制作(C#)
- VS2005 ASP.NET2.0安装项目的制作(包括数据库创建、站点创建、IIS属性修改、Web.Config文件修改)
- C#生成安装文件后自动附加数据库的思路跟算法
- InstallShield 6.3 安装文件制作要点
- VS2005 ASP.NET2.0安装项目的制作(包括数据库创建、站点创建、IIS属性修改、Web.Config文件修改)
- InstallShield制作安装包之获取安装路径并写入到配置文件
- 【转】 VS2005 ASP.NET2.0安装项目的制作(包括数据库创建、站点创建、IIS属性修改、Web.Config文件修改)
- 用InstallShield制作数据库应用系统的安装程序(2000年8月17日) 作者:新疆 宗宏斌
- 用InstallShield制作数据库应用系统的安装程序(2000年8月17日) 作者:新疆 宗宏斌
- C#生成安装文件后自动附加数据库的思路跟算法
- 用InstallShield制作数据库应用系统的安装程序(2000年8月17日) 作者:新疆 宗宏斌
- VS制作安装程序,包含mdb数据库文件
- C#生成安装文件后自动附加数据库的思路跟算法
- InstallShield6.3安装文件制作要点
- vs打包安装时候附加数据库并修改配置文件及文件夹权限