其中第一點經過摸索和總結,有兩種方法:1、直接使用自定義的函數,聲稱sql語句,然後調用osql.exe來執行。
函數如下:
function number CreateDataBase(svSQLsvr,svSQLusr,svSQLpwd)
STRING szCmdLine,szWaitTxt;
begin
szWaitTxt=" 正在創建所需數據庫.";
SdShowMsg (szWaitTxt, TRUE);
Delay(2);
if(g_bWinLogin) 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;
然後在OnEnd中執行如下代碼:
if !MAINTENANCE then
CreateDataBase(g_szServer,g_szUser,g_szPassword);
endif;
其中,g_szServer,g_szUser,g_szPassword分別爲,在installshield中數據庫設置界面(或者自定義窗口)中操作得到的服務器地址,用戶名和密碼。
2、直接在 安裝設計視圖中創建連接後,直接創建一個sqlscript,會在安裝的時候直接執行。這樣的話就的提前知道安裝的位置(這是不可能的),於是就直接寫成可替換的字符串,然後使用 text replacement 選項卡,設置替換項。其中,sqlscript如下:
USE master;
GO
-- Drop database if it exists.
IF EXISTS (SELECT name FROM sys.databases WHERE NAME = 'dmydb')
DROP DATABASE dmydb;
GO
CREATE DATABASE dlbj ON PRIMARY
(FILENAME =
'%TEXT_TO_BE_REPLACED_IN_THE_FILE%/dmydb.mdf')
LOG ON (FILENAME =
'%TEXT_TO_BE_REPLACED_IN_THE_FILE%/dmydb_log.LDF')
FOR ATTACH;
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;
即可。
本文爲轉貼,原帖地址爲:http://www.cnblogs.com/culturenet/archive/2006/12/19/596930.html