CSDN博客

img flyelf

InstallShield自定义对话框浅谈(三)

发表于2004/2/7 22:51:00  1937人阅读

 

/*------------------------------------------------------------------------*/

/*                                                                        */

/*   Function:  BuildSQLServerDSN                                         */

/*                                                                        */

/*   Descrip:   use custom sqlserver's dsn dlg to create dsn.             */

/*                                                                        */

/*   Misc:                                                                */

/*                                                                        */

/*------------------------------------------------------------------------*/

function BuildSQLServerDSN(bFirstStep, bAllowNotSet,szTitle)

          BOOL bDone;

          NUMBER nMessage;

          STRING szSQLAddr, szSQLDB, szDBUserName, szDBUserPwd, szSQLDSN;

          SHORT nDSNType;

          STRING svArg[1024];

          STRING svDSN, svAddr, svDB, svUID, svPWD;

          NUMBER nSplitPos;

          HWND hwndDlg;

          BOOL bBuildResult;

          SHORT wErrMsgLen;

          STRING szErrMsg[MAX_PATH + 1];

          LONG dwErrCode;

   begin

      

       bDone = FALSE;

// EzDefineDialog定义一个对话框

       nMessage = EzDefineDialog(DSN_SQLSERVER_DLG, ISUSER, "", DLG_DSN_SQLSERVER);

       if(nMessage = DLG_ERR) then

           MessageBox("不能进行配置SQLSERVER数据源——找不到对话框", SEVERE);

           bDone = TRUE;

       endif;

       while(bDone = FALSE)

           nMessage = WaitOnDialog(DSN_SQLSERVER_DLG);

           switch(nMessage)

           case DLG_INIT:

                        hwndDlg = CmdGetHwndDlg(DSN_SQLSERVER_DLG);

               CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

               if(bFirstStep) then

                   _WinSubEnableControl(hwndDlg, SD_PBUT_BACK, 0);

               endif;

               _WinSubSetWindowTitle(hwndDlg, szTitle);

               CtrlSetState(DSN_SQLSERVER_DLG, IDC_RADIO_DSN_USER, BUTTON_CHECKED);

              

           case DLG_ERR:

               MessageBox("不能显示配置SQLSERVER数据源对话框", SEVERE);

               bDone = TRUE;

              

           case CANCEL:

               bDone = TRUE;

              

           case DLG_CLOSE:

               bDone = TRUE;

              

           case SD_PBUT_CONTINUE:

               // 调用接口添加一个dsn

               // ...

               CtrlGetText(DSN_SQLSERVER_DLG, IDC_EDIT_DB_ADDR, szSQLAddr);

               CtrlGetText(DSN_SQLSERVER_DLG, IDC_EDIT_SQLSERVER_DB, szSQLDB);

               CtrlGetText(DSN_SQLSERVER_DLG, IDC_EDIT_SQLSERVER_USER_NAME, szDBUserName);

               CtrlGetText(DSN_SQLSERVER_DLG, IDC_EDIT_SQLSERVER_USER_PWD, szDBUserPwd);

               CtrlGetText(DSN_SQLSERVER_DLG, IDC_EDIT_SQLSERVER_DSN, szSQLDSN);

               if(StrLength(szSQLAddr) = 0) then

                   if(AskYesNo("没有输入服务器,是否以后进行配置?", YES) = NO) then

                       _WinSubFocusControl(hwndDlg, IDC_EDIT_DB_ADDR);

                   else

                       bDone = TRUE;

                   endif;

               elseif(StrLength(szSQLDB) = 0) then

                   if(AskYesNo("没有输入数据库,是否以后进行配置?", YES) = NO) then

                       _WinSubFocusControl(hwndDlg, IDC_EDIT_SQLSERVER_DB);

                   else

                       bDone = TRUE;

                   endif;

               elseif(StrLength(szDBUserName) = 0) then

                   if(AskYesNo("没有输入数据库用户,是否以后进行配置?", YES) = NO) then

                       _WinSubFocusControl(hwndDlg, IDC_EDIT_SQLSERVER_USER_NAME);

                   else

                       bDone = TRUE;

                   endif;

               elseif(StrLength(szSQLDSN) = 0) then

                   if(AskYesNo("没有输入数据源名称,是否以后进行配置?", YES) = NO) then

                       _WinSubFocusControl(hwndDlg, IDC_EDIT_SQLSERVER_DSN);

                   else

                       bDone = TRUE;

                   endif;

               else

                   // 已经获取了所有数据,可以调用SQLConfigDataSource添加数据源了

                   // 组织语句

                   // 注:由于DSN不保存UIDPWD,所以在Attribute中不能加入这两个

                   //    关键字,并且每个关键子之间用'/0'分割,由于InstallShield

                   //    中不支持一个字符串中间存在'/0',因此,不能直接用+连接这些

                   //    关键字,下面是一个变通的方法

                   if(CtrlGetState(DSN_SQLSERVER_DLG, IDC_RADIO_DSN_USER) = BUTTON_CHECKED) then

                       nDSNType = ODBC_ADD_DSN;

                   else

                       nDSNType = ODBC_ADD_SYS_DSN;

                   endif;

                   svDSN = "DSN=" + szSQLDSN;

                   svAddr = "SERVER=" + szSQLAddr;

                   svDB = "DATABASE=" + szSQLDB;

                   svUID = "UID=" + szDBUserName;

                   svPWD = "PWD=" + szDBUserPwd;

                   svArg = svDSN + ' ' + svAddr + ' ' + svDB;

                   nSplitPos = StrLength(svDSN);

                   svArg[nSplitPos] = '/0';

                   nSplitPos += StrLength(svAddr) + 1;

                   svArg[nSplitPos] = '/0';

                   nSplitPos += StrLength(svDB) + 1;

                   svArg[nSplitPos] = '/0';

                   // 调用ODBCCP32中的SQLConfigDataSource添加一个DSN

                   // 如果返回FALSE,表示添加失败,这时可以调用

                   // SQLInstallerError来得到失败的原因

                   bBuildResult = SQLConfigDataSource(NULL, nDSNType, "SQL Server", svArg);

                   if(bBuildResult) then

                           // 如果添加成功

                           bDone = TRUE;

                       else

                           // 添加失败                                                            

                           wErrMsgLen = MAX_PATH;

                           SQLInstallerError(1, &dwErrCode, szErrMsg, wErrMsgLen, &wErrMsgLen);

                           if(bAllowNotSet) then

                                szErrMsg = "配置数据源失败——" + szErrMsg + "!是否以后进行配置?";

                               if(AskYesNo(szErrMsg, YES) = NO) then

                                   bDone = FALSE;

                               else

                                   bDone = TRUE;

                               endif;

                           else

                               szErrMsg = "配置数据源失败——" + szErrMsg + "";

                               MessageBox(szErrMsg, SEVERE);

                           endif;

                       endif;

               endif;

              

           case SD_PBUT_BACK:

               // 上一步

               bDone = TRUE;

              

           case SD_PBUT_EXITSETUP:

               bDone = TRUE;

          

           case IDC_RADIO_DSN_USER:

               CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

          

           case IDC_RADIO_DSN_SYSTEM:

               CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

          

           case IDC_EDIT_DB_ADDR:

               CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

         

           case IDC_EDIT_SQLSERVER_USER_NAME:

               CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

          

           case IDC_EDIT_SQLSERVER_USER_PWD:

               CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

         

           case IDC_EDIT_SQLSERVER_DSN:

               CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

          

           case IDC_EDIT_SQLSERVER_DB:

               CheckSQLServerDSNInputValid(hwndDlg, bAllowNotSet);

           

           endswitch;

          

       endwhile;

      

       EndDialog(DSN_SQLSERVER_DLG);

       ReleaseDialog(DSN_SQLSERVER_DLG);

      

       if(nMessage = SD_PBUT_CONTINUE) then

           return NEXT;

       elseif(nMessage = SD_PBUT_BACK) then

           return BACK;

       else

           Do(EXIT);

       endif;

   end;
0 0

相关博文

我的热门文章

img
取 消
img