Reference: http://blog.csdn.net/notavailabe/archive/2005/12/13/551757.aspx
MSDE databases are often used in our software. After software development is complete, we usually need to package MSDE and our software for distribution and implement automatic database installation and configuration, these functions are usually implemented in the installer. The script below implements automatic installation of the MSDE database using the installer Installshield and automatic configuration of ODBC.
//////////////////////////////////////// ////////////////////////////////////////
//
// File Name: Setup. rul
//
// Description: InstallShield script
//
// Comments: This script was generated based on the selections you made in
// The Project Wizard. Refer to the help topic entitled "Modify
// The script that the Project Wizard generates "for information
// On possible next steps.
//
//////////////////////////////////////// ////////////////////////////////////////
// Include header files
# Include "ifx. h"
/// // String defines //////////////// ////////////
/// // Installation declarations ///////////////////
// ----- DLL function prototypes -----
// Your DLL function prototypes
Prototype ODBCCP32.SQLConfigDataSource (HWND, SHORT, STRING, STRING );
Prototype ODBCCP32.SQLInstallerError (SHORT, byref long, byref string, SHORT, byref short );
Prototype ISODBCDisplaySQLInstallerErrorMsg (STRING );
Prototype AddODBC (NUMBER );
# Define SQL _MAX_MESSAGE_LENGTH 512
# Define SQL _SUCCESS_WITH_INFO 1
# Define SQL _NO_DATA 100
# Define SQL _ERROR-1
# Define ODBC_ADD_SYS_DSN 4 // add a system DSN
# Define ODBC_REMOVE_SYS_DSN 6 // remove a system DSN
// ---- Script function prototypes -----
Prototype CreateDataBase (string, string, string );
// Your script function prototypes
// Your global variables
//////////////////////////////////////// //////////////////////////////////////// //////////
//
// FUNCTION: OnFirstUIBefore
//
// EVENT: The OnFirstUIBefore event is called by OnShowUI when the setup is
// Running in first install mode. By default this event displays UI allowing
// The end user to specify installation parameters.
//
// Note: This event will not be called automatically in
// Program... endprogram style setup.
//
//////////////////////////////////////// //////////////////////////////////////// //////////
Function OnFirstUIBefore ()
Number nResult;
Number nLevel;
Number nvSize, nSetupType;
Number nLoop;
String szTitle, szMsg;
String szOpt1, szOpt2, szLicenseFile;
String szName, szCompany;
String szTargetPath;
String svDir;
String szComponents, szTargetdir;
String szTargetDirAppendix;
BOOL bLicenseAccepted;
Begin
SetTitle (@ TITLE_MAIN, 24, WHITE );
SetTitle (@ TITLE_CAPTIONBAR, 0, BACKGROUNDCAPTION );
Enable (FULLWINDOWMODE );
SetColor (BACKGROUND, RGB (58,110,165 ));
NSetupType = COMPLETE;
If (ALLUSERS) then
TARGETDIR = PROGRAMFILES ^ IFX_COMPANY_NAME ^ IFX_PRODUCT_NAME;
Else
TARGETDIR = FOLDER_APPDATA ^ IFX_COMPANY_NAME ^ IFX_PRODUCT_NAME;
Endif;
// Customize the default TARGETDIR for multi-instance application.
// TODO: If you want something different customize the code below.
If (MAINT_OPTION = MAINT_OPTION_MULTI_INSTANCE & MULTI_INSTANCE_COUNT> 0) then
NLoop = 1;
SvDir = TARGETDIR;
While (ExistsDir (TARGETDIR) = EXISTS)
NumToStr (szTargetDirAppendix, nLoop );
TARGETDIR = svDir + szTargetDirAppendix;
NLoop = nLoop + 1;
Endwhile;
Endif;
SvDir = TARGETDIR;
SzName = "";
SzCompany = "";
BLicenseAccepted = FALSE;
// Beginning of UI Sequence
Dlg_Start:
Dlg_SdWelcome:
SzTitle = "";
SzMsg = "";
NResult = SdWelcome (szTitle, szMsg );
If (nResult = BACK) goto Dlg_Start;
Dlg_SdLicense2:
SzTitle = "";
SzOpt1 = "";
SzOpt2 = "";
SzLicenseFile = SUPPORTDIR ^ "license.txt ";
NResult = SdLicense2 (szTitle, szOpt1, szOpt2, szLicenseFile, bLicenseAccepted );
If (nResult = BACK) then
Goto Dlg_SdWelcome;
Else
BLicenseAccepted = TRUE;
Endif;
Dlg_SdRegisterUser:
SzMsg = "";
SzTitle = "";
NResult = SdRegisterUser (szTitle, szMsg, szName, szCompany );
If (nResult = BACK) goto Dlg_SdLicense2;
Dlg_SetupType2:
SzTitle = "";
SzMsg = "";
NResult = SetupType2 (szTitle, szMsg, "", nSetupType, 0 );
If (nResult = BACK) then
Goto Dlg_SdRegisterUser;
Else
NSetupType = nResult;
If (nSetupType! = CUSTOM) then
SzTargetPath = TARGETDIR;
NvSize = 0;
FeatureCompareSizeRequired (MEDIA, szTargetPath, nvSize );
If (nvSize! = 0) then
MessageBox (szSdStr_NotEnoughSpace, WARNING );
Goto Dlg_SetupType2;
Endif;
Endif;
Endif;
Dlg_SdAskDestPath2:
If (nResult = BACK) & (nSetupType! = CUSTOM) goto Dlg_SetupType2;
SzTitle = "";
SzMsg = "";
If (nSetupType = CUSTOM) then
NResult = SdAskDestPath2 (szTitle, szMsg, svDir );
TARGETDIR = svDir;
Endif;
If (nResult = BACK) goto Dlg_SetupType2;
Dlg_SdComponentTree:
If (nResult = BACK) & (nSetupType! = CUSTOM) goto Dlg_SdAskDestPath2;
SzTitle = "";
SzMsg = "";
SzTargetdir = TARGETDIR;
SzComponents = "";
NLevel = 2;
If (nSetupType = CUSTOM) then
NResult = SdFeatureTree (szTitle, szMsg, szTargetdir, szComponents, nLevel );
If (nResult = BACK) goto Dlg_SdAskDestPath2;
Endif;
Dlg_ObjDialogs:
NResult = ShowObjWizardPages (nResult );
If (nResult = BACK) goto Dlg_SdComponentTree;
Dlg_SdStartCopy2:
SzTitle = "";
SzMsg = "";
NResult = SdStartCopy2 (szTitle, szMsg );
If (nResult = BACK) goto Dlg_ObjDialogs;
Return 0;
End;
//---------------------------------------------------------------------------
// OnFirstUIAfter
//
// First Install UI Sequence-After Move Data
//
// The OnFirstUIAfter event called by OnShowUI after the file transfer
// Of the setup when the setup is running in first install mode. By default
// This event displays UI that informs the end user that the setup has been
// Completed successfully.
//
// Note: This event will not be called automatically in
// Program... endprogram style setup.
//---------------------------------------------------------------------------
Function OnFirstUIAfter ()
STRING szTitle, szMsg1, szMsg2, szOpt1, szOpt2;
String szSQLsvr, szSQLusr, szSQLpwd; // added
String szServiceName, szServiceDisplayName, szServiceDescription, szServicePathFile, szStartServiceArgs;
String szTemp;
String szMsde2000, szw.line;
String szWaitTxt;
BOOL bStartService;
NUMBER bvOpt1, bvOpt2;
Number svServiceState;
/******************************/
String szName, szValue;
Number nType;
/**************************/
Begin
// Install MSDE2000
SzWaitTxt = "MSDE2000 is being installed. Please wait ...";
SdShowMsg (szWaitTxt, TRUE );
SzMsde2000 = SRCDIR ^ "MSDE" ^ "setup.exe ";
Szw.line = "/q/wait SECURITYMODE = SQL ";
If (LaunchAppAndWait (szMsde2000, szCmdLine, LAAW_OPTION_WAIT | LAAW_OPTION_MAXIMIZED) <0) then
MessageBox ("MSDE2000 Installation failed. \ n. If the problem persists, contact the system supplier! ", SEVERE );
Abort;
Endif;
SdShowMsg (szWaitTxt, FALSE );
// Start the service
SzServiceName = "MSSQL $ TEST ";
/* SzServiceDisplayName = "MSSQL $ TEST ";
SzServiceDescription = "MSSQLServer ";
SzServicePathFile = PROGRAMFILES ^ "Microsoft SQL Server" ^ "MSSQL $ TEST" ^ "Binn" ^ "sqlservr.exe-sTEST ";
BStartService = TRUE;
SzTemp = PROGRAMFILES ^ "Microsoft SQL Server" ^ "MSSQL $ TEST ";
SzStartServiceArgs = "-c" + "-d" + szTemp ^ "Data" ^ "master. mdf" +
"-E" + szTemp ^ "LOG" ^ "errorlog" +
"-L" + szTemp ^ "Data" ^ "mastlog. ldf ";
ServiceAddService (szServiceName, szServiceDisplayName, szServiceDescription, szServicePathFile, bStartService,
SzStartServiceArgs );*/
SzStartServiceArgs = "";
If (ServiceGetServiceState (szServiceName, svServiceState) <ISERR_SUCCESS) then
MessageBox ("Get service" + szServiceName + "error, the program will try to start the service. ", SEVERE );
If (ServiceStartService (szServiceName, szStartServiceArgs) <ISERR_SUCCESS) then
MessageBox ("Start Service" + szServiceName + "error. ", SEVERE );
Endif;
Else
If (svServiceState! = SERVICE_RUNNING) then
If (ServiceStartService (szServiceName, szStartServiceArgs) <ISERR_SUCCESS) then
MessageBox ("Start Service" + szServiceName + "error. ", SEVERE );
Endif;
Endif;
Endif;
// Load your own database
SzSQLsvr = "(local)" ^ "TEST ";
SzSQLusr = "sa ";
SzSQLpwd = "sa ";
CreateDataBase (szSQLsvr, szSQLusr, szSQLpwd );
// Create a system DSN
AddODBC (ODBC_ADD_SYS_DSN );
// Set application information in the Registry
SzName = "CurrentVersion ";
SzValue = IFX_PRODUCT_VERSION;
NType = REGDB_STRING;
If (RegDBSetAppInfo (szName, nType, szValue,-1) <0) then
MessageBox ("An error occurred while setting the application version information in the Registry", WARNING );
Endif;
SzName = "ProductKey ";
SzValue = IFX_PRODUCT_KEY;
NType = REGDB_STRING;
If (RegDBSetAppInfo (szName, nType, szValue,-1) <0) then
MessageBox ("An error occurred while setting the application name information in the Registry", WARNING );
Endif;
ShowObjWizardPages (NEXT );
SzTitle = "";
SzMsg1 = "";
SzMsg2 = "";
SzOpt1 = "";
SzOpt2 = "";
BvOpt1 = FALSE;
BvOpt2 = FALSE;
If (BATCH_INSTALL) then
SdFinishReboot (szTitle, szMsg1, SYS_BOOTMACHINE, szMsg2, 0 );
Else
// If the update service is enabled, show finish dialog that has des
// Update check option.
If (ENABLED_ISERVICES & SERVICE_ISUPDATE) then
If (SdFinishUpdateEx (szTitle, szMsg1, szMsg2, szOpt1, szOpt2, TRUE) then
// Don't check for updates in silent mode.
If (MODE! = SILENTMODE) then
UpdateServiceCheckForUpdates ("", FALSE );
Endif;
Endif;
Else
SdFinish (szTitle, szMsg1, szMsg2, szOpt1, szOpt2, bvOpt1, bvOpt2 );
Endif;
Endif;
End;
//////////////////////////
// Create a database added at 2003/07/23
//////////////////////////
Function CreateDataBase (szSQLsvr, szSQLusr, szSQLpwd)
String szCmd, szCmdLine, and szWaitTxt;
Begin
SzWaitTxt = "creating the required database ....";
SdShowMsg (szWaitTxt, TRUE );
Delay (2 );
SzCmdLine = "-U" + szSQLusr + "-P" + szSQLpwd + "-S" + szSQLsvr + "-Q \" exec sp_detach_db 'test '\"";
If (LaunchAppAndWait ("osql.exe", szCmdLine, LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN) <0) then
MessageBox ("failed to detach the original database! If Microsoft SQL Server 2000 is installed in your system or the service is started. \ n cannot be solved, contact the system supplier! ", SEVERE );
Endif;
SzCmdLine = "-U" + szSQLusr + "-P" + szSQLpwd + "-S" + szSQLsvr + "-Q \" exec sp_attach_db 'test ', '"+ TARGETDIR ^" data"
^ "TEST_data.mdf ','" +
TARGETDIR ^ "data" ^ "TEST_log.ldf '\ 0 ";
If (LaunchAppAndWait ("osql.exe", szCmdLine, LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN) <0) then
MessageBox ("database creation failed! If Microsoft SQL Server 2000. \ n has been installed in your system and cannot be solved, contact the system supplier! ", SEVERE );
Endif;
SdShowMsg (szWaitTxt, FALSE );
Return 0;
End;
//////////////////////////////////////// /////////////////////
Function AddODBC (nvType)
HWND hwnd;
NUMBER nRes, nError, nvLen, nCount;
STRING szArgs, szDBQ, szDriver;
Begin
UseDLL (WINSYSDIR ^ "odbccp32.dll ");
SzDriver = "SQL Server ";
// Parameter Network = dbnmpntw ??? Dbmssocn
/* SzArgs = "DSN = test |" +
"Description = SQLConfigDSN Sample |" +
"SERVER = (local) |" +
"ADDRESS = 192.168.7.199 |" +
"NETWORK = dbmssocn |" +
"DATABASE = pubs | ";*/
SzArgs = "DSN = TEST |" +
"Description = SQLConfigDSN Sample |" +
"SERVER = (local)" ^ "TEST |" +
"DATABASE = TEST | ";
// Replace all semicolons with NULL characters to create null-delimited,
// Double-NULL terminated string, which is the format needed to pass to SQL functions.
SzArgs = szArgs + '| ;';
NvLen = StrLength (szArgs );
NvLen = nvLen-1;
NCount = 0;
For nCount = 0 to nvLen
If (szArgs [nCount] = '|') then
SzArgs [nCount] = '\ 0 ';
Endif;
Endfor;
// Hwnd = getjavaswhandle (HWND_INSTALL );
NRes = SQLConfigDataSource (hwnd, nvType, szDriver, szArgs );
If (nRes = FALSE) then
NError = ISODBCDisplaySQLInstallerErrorMsg ("whocares ");
Endif;
UnUseDLL (WINSYSDIR ^ "odbccp32.dll ");
End;
//////////////////////////////////////// /////////////////////
Function ISODBCDisplaySQLInstallerErrorMsg (szODBCComponent)
LONG fErrorCode;
NUMBER nvResult;
STRING szErrorMsg [SQL _MAX_MESSAGE_LENGTH];
SHORT cbErrorMsg, cbErrorMsgMax, iError;
POINTER pfErrorCode, pcbErrorMsg;
Begin
PfErrorCode = & fErrorCode;
PcbErrorMsg = & cbErrorMsg;
IError = 1;
CbErrorMsgMax = SQL _MAX_MESSAGE_LENGTH;
NvResult = SQLInstallerError (iError, fErrorCode, szErrorMsg,
CbErrorMsgMax, pcbErrorMsg );
If (TRUE) then
If (nvResult = SQL _NO_DATA | nvResult = SQL _ERROR) then
MessageBox (@ ISODBC_ERR_NOERRDATA + "\ n" + szODBCComponent, SEVERE );
Elseif (nvResult = SQL _SUCCESS_WITH_INFO) then
SprintfBox (SEVERE, "", @ ISODBC_ERR_ERRBUFFSHORT + "\ n" +
@ ISODBC_ERR_NUMBER + "\ n" + szODBCComponent, fErrorCode );
Else
MessageBox (szErrorMsg + "\ n" + szODBCComponent, SEVERE );
Endif;
Endif;
Return (fErrorCode );
End;
//---------------------------------------------------------------------------
// OnBegin
//
// The OnBegin event is called directly by the framework after the setup
// Initializes.
//
// Note: This event will not be called automatically in
// Program... endprogram style setup.
//---------------------------------------------------------------------------
Function OnBegin ()
/******************************/
Number nRootKey;
String szKeySQLServer;
String szKey, szRegName, svValue;
Number nvType, nvRegSize;
/******************************/
Begin
If (! MAINTENANCE) then
/*************************************** *********/
// Check whether the previous version of the program has been installed
NRootKey = HKEY_LOCAL_MACHINE;
SzKey = "SOFTWARE \ TEST" ^ IFX_PRODUCT_NAME;
If (RegDBSetDefaultRoot (nRootKey) <0) then
MessageBox ("failed to call RegDBSetDefaultRoot function. ", SEVERE );
Endif;
If (RegDBKeyExist (szKey) = 1) then
MessageBox ("the installer detects that the system has installed the previous version of this program. \ n uninstall the previous version before continuing this installation. ", WARNING );
Abort;
Endif;
/*************************************** *********/
Endif;
End;
// --- Include script file section ---
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/notavailabe/archive/2005/12/13/551757.aspx