使用installshield在安装过程中任意步骤安装MS sql数据库

 由于安装时用户可以选择安装软件1、安装软件2、安装软件3、安装数据库等多个选项,用户可以选择是否安装数据,而且只有选择安装数据库才进行数据库的安装。

这样要在安装包中自己来对数据库的安装进行定制,而installshield 2008中默认的 数据库安装方式是在一开始就运行的。不能选择不安装。

在网上查了许多资料,参考http://community.acresso.com/showthread.php?t=156641以及网上其他的资料。最终解决了此问题。

我的工程环境为:

Installshield2008

工程为Installscript MSI Project.(选择installScript工程在编译时出现了函数未定义的错误,有时间再做installscript工程的。)

操作系统为windows2003 server.

1、加.obl文件到工程。

在build菜单->settings->Compile/Link选项卡中.

如下图

在link libraries中加入以下两行(如果他们不存在),以英文逗号分隔。

  1. <ISProductFolder>/Script/SQLRuntime/Lib/SQLRT.obl
  2. <ISProductFolder>/Script/SQLRuntime/Lib/SQLCONV.obl

2、加入相应的dll到Binary Table,在Additional Tools->Direct editor->Binary

在第一个输入框中输入名字SQLRT.DLL,在第二个输入框中,点浏览找到<ProgramFiles>/InstallShieldX/Redist/Compressed Files/Language Independent/Intel 32/SQLRT.dll.点确定dll被加入到表中。

同样在新的一行第一个输入框中ISSQLSRV.DLL,在第二个输入框中,点浏览找到

<ProgramFiles>/InstallShieldX/Redist/Language Independent/i386/ISSQLSrv.dll,点确定dll被加入到表中。

如下图

 

 

 

 

 3、写脚本。

 

//===========================================================================
//
//  File Name:    Setup.rul
//
//  Description:  Blank setup main script file
//
//  Comments:     Blank setup is an empty setup project. If you want to
//      create a new project via. step-by step instructions use the
//      Project Assistant.
//
//===========================================================================

// Included header files ----------------------------------------------------
#include "ifx.h"       

export prototype CreatDBScript_Installing();
prototype  CreateDataBase(STRING,STRING,STRING,BOOL);                         
  
//---------------------------------------------------------------------------
// The Installing event is sent after the feature CreatDBScript
// is installed.
//---------------------------------------------------------------------------
function CreatDBScript_Installing()
 string sMessage,sDLL;
 string szConnection, szServer, szUser, szPassword, szDB, sTemp[MAX_PATH];
 number nResult, nSize, nCount,nBtn;
 BOOL   bWinLogin, bNext;
 LIST   listConnections;
begin
    sDLL = SUPPORTDIR ^ "SQLRT.DLL";
 nResult = StreamFileFromBinary( ISMSI_HANDLE, "SQLRT.DLL", sDLL );
 UseDLL(sDLL);

    //First initialize SQL Server runtime
    SQLRTInitialize2();

   // Suppress ISSQLSRV.DLL to show a connection error message.
 MsiSetProperty( ISMSI_HANDLE, "IS_SQLSERVER_CA_SILENT", "1" );

    //Get the names of all the necessary connections
    listConnections = SQLRTGetConnections();
    ListGetFirstString (listConnections, szConnection);

    nCount = 0;

    //determine if NEXT or BACK will be returned
    //if there are no connections to make
    if( nBtn != BACK ) then
        bNext = TRUE;
    else
        bNext = FALSE;
  //start at end if going BACK
  while (ISERR_SUCCESS = ListGetNextString( listConnections, szConnection ) );
   nCount++;
  endwhile;
    endif;

    sDLL = SUPPORTDIR ^ "ISSQLSRV.DLL";
 nResult = StreamFileFromBinary( ISMSI_HANDLE, "ISSQLSRV.DLL", sDLL );
 UseDLL(sDLL);
    // Login for each connection
    while (nResult = ISERR_SUCCESS)

        //Get Default values for connection
        SQLRTGetConnectionInfo( szConnection, szServer, szDB, szUser, szPassword );

        bWinLogin = SQLRTGetConnectionAuthentication( szConnection );

        // Display login dialog (without connection name)
        // COMMENT OUT TO SWAP DIALOGS
        nResult = SQLServerSelectLogin2( szConnection, szServer, szUser, szPassword, bWinLogin, szDB, FALSE, TRUE );

        // Display login dialog (with connection name)
        // UNCOMMENT TO SWAP DIALOGS
        // nResult = SQLServerSelectLogin2( szConnection, szServer, szUser, szPassword, bWinLogin, szDB, TRUE, TRUE );

        if( nResult = NEXT ) then

            //store data in case we need it again
            SQLRTPutConnectionInfo2( szConnection, szServer, szDB, szUser, szPassword );

         SQLRTPutConnectionAuthentication( szConnection, bWinLogin );

            //test connection
   nResult = SQLRTTestConnection2( szConnection, szServer, szDB, szUser, szPassword, bWinLogin );

            nSize = MAX_PATH;
            MsiGetProperty( ISMSI_HANDLE, "IS_SQLSERVER_STATUS", sTemp, nSize );

           if( sTemp != "0" ) then

    nSize = _MAX_PATH;
    MsiGetProperty( ISMSI_HANDLE, "IS_SQLSERVER_STATUS_ERROR", sMessage, nSize );
                   
    if( nSize = 0 ) then
           Sprintf(sMessage, SdLoadString( IDS_IFX_SQL_ERROR_LOGIN_FAILED ), szConnection, SdLoadString( ISCRIPT_E_UNKNOWN_ERROR_DESC ));
    endif;
    
    MessageBox( sMessage, MB_OK );

                //Show same login dialog again
                nResult = ListCurrentString(listConnections, szConnection);

            else //SUCCESS
               
                //Move on to next connection
                nCount++;
                bNext = TRUE;
                nResult = ListGetNextString(listConnections, szConnection);
                CreateDataBase( szServer, szUser, szPassword, bWinLogin ) ;
            endif;

        else

            //BACK
            nCount--;
            bNext = FALSE;
            nResult = ListSetIndex( listConnections, nCount );
            ListCurrentString( listConnections, szConnection );

        endif;

    endwhile;
 
    if( bNext ) then
        return NEXT;
    else
        return BACK;
    endif;
end;       

function number CreateDataBase(svSQLsvr,svSQLusr,svSQLpwd,bWindowsLogin)
 STRING szCmdLine,szWaitTxt;
begin
 szWaitTxt=" 正在创建所需数据库.";
 SdShowMsg (szWaitTxt, TRUE);
 Delay(2);
 
  if(bWindowsLogin) then
     szCmdLine = "/E  /S "+svSQLsvr+" /Q /"EXEC  sp_attach_db  @dbname  =  N'dlbj',@filename1  = N'"+TARGETDIR ^"mydb.MDF',@filename2  = N'"+TARGETDIR ^"dlbj_web_log.LDF'/"";
  else
     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'/"";
  endif;
 //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'/"";
 
 if (LaunchAppAndWait("osql.exe", szCmdLine,WAIT) < 0) then
  MessageBox ("数据库创建失败!请确您的系统中已安装 Microsoft SQL Server 2000. 如仍无法解决,请联系系统供应商!",SEVERE);
 endif;
 SdShowMsg (szWaitTxt, FALSE);
 szWaitTxt=" 正在优化系统数据库.";
 SdShowMsg (szWaitTxt, TRUE);
 Delay(2);
 szCmdLine = "/U "+svSQLusr+" /P "+svSQLpwd+" /S "+svSQLsvr+" /Q /"use dlbj ; exec sp_updatestats/"";
 if (LaunchAppAndWait("osql.exe", szCmdLine,WAIT) < 0) then
  MessageBox ("数据库优化失败!您可以在 sql查询分析器中执行 use dlbj ; exec sp_updatestats 完成!",SEVERE);
 endif;
 SdShowMsg (szWaitTxt, FALSE); 

 return 0;
end;      
               

4、设置数据库安装位置

在Organization中Features加入soft1,soft2,soft3,DB等。现在要把数据库安装与DB相关联,只有用户选择数据库安装才进行数据库的安装。

在右侧的 DB Program Feature中的Display name中输入相应的安装说明,如数据库安装.

在OnInStalled中,在下拉框中选择CreatDBScript_Installing,如果没有出现在这,看函数前是否加了export关键字。

如下图

 

这样就完成了。

另外也参考网上http://www.cnblogs.com/culturenet/archive/2006/12/19/596930.html

以及installshield自己生成的代码。

 在以后的安装实践中,发现会出现对话框不退出的情况,没有好的办法解决。

只好把它又在原来地方,用判断来确定是否出现对话框了。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunnf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值