最近又一个项目接近尾声,不过对方要求安装必须傻瓜化,尽量不用手动配置。于是就的重新捡起两三年没有用过的installshield了。找到 installshield 12 于是,着手开始制作安装包。因为是网站项目,所以,其他方面倒是问题不大,因为当年用installshield 6.2的时候,也是相当熟练的。最主要的有两个方面:一、数据库的安装,因为我的数据库里面有许多存储过程和初始数据,所以不想直接用script来初始化。于是就想直接在安装包中把数据库文件和日志文件打包,然后在安装时直接附加就行。二、修改web.config文件中的数据库连接字符串和一些相关的参数,比如一些路径相关的参数等等。
其中第一点经过摸索和总结,有两种方法:1、直接使用自定义的函数,声称sql语句,然后调用osql.exe来执行。函数如下:
None.gif function number CreateDataBase(svSQLsvr,svSQLusr,svSQLpwd)
None.gif STRING szCmdLine,szWaitTxt;
None.gifbegin
None.gifszWaitTxt = " 正在创建所需数据库dot.gif. " ;
None.gifSdShowMsg (szWaitTxt, TRUE );
None.gifDelay( 2 );
None.gif
None.gif if (g_bWinLogin) then
None.gif szCmdLine = " /E /S " + svSQLsvr + " /Q \ " EXEC sp_attach_db @dbname = N ' dlbj',@filename1 = N'"+TARGETDIR ^"mydb.MDF',@filename2 = N'"+TARGETDIR ^"dlbj_web_log.LDF'\"";
None.gif else
None.gif 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'\"";
None.gif endif;
None.gif // 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'\"";
None.gif
None.gif if (LaunchAppAndWait( " osql.exe " , szCmdLine,WAIT) < 0 ) then
None.gifMessageBox ( " 数据库创建失败!请确您的系统中已安装 Microsoft SQL Server 2000. 如仍无法解决,请联系系统供应商! " ,SEVERE);
None.gifendif;
None.gifSdShowMsg (szWaitTxt, FALSE );
None.gifszWaitTxt = " 正在优化系统数据库dot.gif. " ;
None.gifSdShowMsg (szWaitTxt, TRUE );
None.gifDelay( 2 );
None.gifszCmdLine = " /U " + svSQLusr + " /P " + svSQLpwd + " /S " + svSQLsvr + " /Q \ " use dlbj ; exec sp_updatestats \ "" ;
None.gif if (LaunchAppAndWait( " osql.exe " , szCmdLine,WAIT) < 0 ) then
None.gifMessageBox ( " 数据库优化失败!您可以在 sql查询分析器中执行 use dlbj ; exec sp_updatestats 完成! " ,SEVERE);
None.gifendif;
None.gifSdShowMsg (szWaitTxt, FALSE );
None.gif
None.gifreturn 0 ;
None.gif
None.gif end ;
然后在OnEnd中执行如下代码:
None.gif if !MAINTENANCE then
None.gif
None.gifCreateDataBase(g_szServer,g_szUser,g_szPassword);
None.gifendif;
其中,g_szServer,g_szUser,g_szPassword分别为,在installshield中数据库设置界面(或者自定义窗口)中操作得到的服务器地址,用户名和密码。2、直接在 安装设计视图中创建连接后,直接创建一个sqlscript,会在安装的时候直接执行。这样的话就的提前知道安装的位置(这是不可能的),于是就直接写成可替换的字符串,然后使用 text replacement 选项卡,设置替换项。其中,sqlscript如下:
None.gif USE master;
None.gif GO
None.gif -- Drop database if it exists.
None.gif IF EXISTS ( SELECT name FROM sys.databases WHERE NAME = ' dmydb ' )
None.gif DROP DATABASE dmydb;
None.gif GO
None.gif CREATE DATABASE dlbj ON PRIMARY
None.gif (FILENAME =
None.gif ' %TEXT_TO_BE_REPLACED_IN_THE_FILE%\dmydb.mdf ' )
None.gif LOG ON (FILENAME =
None.gif ' %TEXT_TO_BE_REPLACED_IN_THE_FILE%\dmydb_log.LDF ' )
None.gif FOR ATTACH;
None.gif 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)
NUMBER nResult, nvLineNumber;
STRING WebConfigFile, svReturnLine;
begin
// build the path to
the Web.Config
WebConfigFile = svFilePath ^
"
web.config
"
;
// search the file for
the key we specify
nResult = FileGrep( WebConfigFile, svFindEntry, svReturnLine, nvLineNumber, RESTART );
switch(nResult)
case 0
:
// Since this line normally appears twice in our web.config file, we '
re
// checking
for
the existence of a 2nd match
and
updating it instead.
// If
there
is
no 2nd match, go ahead
and
update the one we find.
if ( svFindEntry =
"
connectionString
"
)
then
nResult = FileGrep( WebConfigFile, svFindEntry , svReturnLine, nvLineNumber, CONTINUE );
if ( nResult <
0
)
then
FileGrep( WebConfigFile, svFindEntry, svReturnLine, nvLineNumber, RESTART );
endif;
endif;
// once we find the key, update ( replace
) it
with
our
new
values
if ( FileInsertLine( WebConfigFile, svNewEntry, nvLineNumber, REPLACE
)
<
0
)
then
// hmmm
we couldn ' t update the value of the key
MessageBox( " Unable to update Web.Config file.
"
, SEVERE );
endif;
case -
2
:
// File Not
Found
MessageBox( " Web.Config file not found. "
, SEVERE );
case -
4
:
// EOF reached
MessageBox( svFindEntry + "
key not found.
"
, SEVERE );
default:
// unknown error
MessageBox( " An unknown error has occurred. The Web.Config file has NOT been updated. "
, SEVERE );
endswitch;
end ;
即可。