如何在Inno Setup中为.net应用程序制作带有数据库的安装文件
本文关键字:作带 数据库 文件 安装 应用程序 Inno Setup net 中为 | 更新日期: 2023-09-27 18:03:43
我找不到任何例子,所以我不确定这是否可能。我想做的是:
我想安装。net c# windows服务与数据库。所以我的要求将是。net框架和SQL Server 2008上的客户端机器。
所以,它看起来应该是这样的:
- 检查是否有。net Framework 4.0和SQL Server 2008
- 如果找不到SQL Server -则要求客户端选择路径或离开。 最终安装。net Framework 4.0
- 登录到SQL Server,并(从脚本)创建表,进程等。
- 从命令行安装wind服务。在这一点上,我还必须在我的app.config中设置连接字符串-这是可能的吗?)
我想在Inno设置中这样做。这可能吗?
好吧,我可以帮你解决第1和第3个问题。
检查。net框架,你可以使用下面的方法(如果需要,它也会安装。net框架)。我目前用它来检查。net 2.0,但你可以改变它寻找的版本来检查4.0。
[Files]
Source: Files'dotnetfx.exe; DestDir: {tmp}; Flags: ignoreversion; Check: NeedsFramework
[Run]
Filename: {tmp}'dotnetfx.exe; Parameters: "/q:a /c:""install /l /q"""; WorkingDir: {tmp}; Flags: skipifdoesntexist; StatusMsg: Installing .NET Framework if needed. This may take several minutes.
[Code]
// .NET install helpers
// Indicates whether .NET Framework 2.0 is installed.
function IsDotNET20Detected(): boolean;
var
success: boolean;
install: cardinal;
begin
success := RegQueryDWordValue(HKLM, 'SOFTWARE'Microsoft'NET Framework Setup'NDP'v2.0.50727', 'Install', install);
Result := success and (install = 1);
end;
//RETURNS OPPOSITE OF IsDotNet20Detected FUNCTION
//Remember this method from the Files section above
function NeedsFramework(): Boolean;
begin
Result := (IsDotNET20Detected = false);
end;
//IF SETUP FINISHES WITH EXIT CODE OF 0, MEANING ALL WENT WELL
//THEN CHECK FOR THE PRESENCE OF THE REGISTRY FLAG TO INDICATE THE
//.NET FRAMEWORK WAS INSTALLED CORRECTLY
//IT CAN FAIL WHEN CUST DOESN'T HAVE CORRECT WINDOWS INSTALLER VERSION
function GetCustomSetupExitCode(): Integer;
begin
if (IsDotNET20Detected = false) then
begin
MsgBox('.NET Framework was NOT installed successfully!',mbError, MB_OK);
result := -1
end
end;
注意,这个解决方案是从本文派生出来的。你可以从微软网站下载。net 4.0版本的dotnetfx。
对于步骤4,我建议您创建一个工具/脚本,将其安装在用户的机器上,然后根据需要从Run部分调用它。
步骤2将是棘手的,但显然不是不可能的。经过一些阅读,似乎可以添加自定义UI页面到InnoSetup。请参阅此处的帮助以了解该方法。我不确定在实际的UI页面中可以做多少工作。
值得注意的是,在InnoSetup中使用Pascal脚本,你或多或少可以完全访问Win32函数,加上实例化COM对象的能力,这可以想象。net库暴露一个COM接口....?
Inno setup将所有的安装文件压缩到一个可执行文件中。当你运行这个可执行文件时,它会提取所有你在inno setup中包含的文件。我不认为它会自动运行你在setup.exe中包含的可执行文件。
或者,您也可以使用wix创建设置。Wix的设置也可以在。net framework 4.0中构建。
这是我用命令行oracle sqlplus执行SQL脚本的示例代码1. 示例代码
[Dirs]
; Create folders at user side
Name: {app}'ScriptLog;
[Code]
///////////////////////////////////////////////////////////
//// Modify Sql function
//// TagName: the script line you want to modify tag name,
//// OldString: the old string,
//// NewString: the new string,
//// StringArr: the script strings array
///////////////////////////////////////////////////////////
function ModifySql(var TagStr, OldStr, NewStr: String; const StringArr: array of String): Boolean;
var
batPath: String;
tmpStr: String;
ResultCode: Integer;
i: Integer;
begin
result := false;
for i:= 0 to GetArrayLength(StringArr)-1 do
// if TagStr and OldStr are in the same line
if ( (StringArr[i] <> '') and (Pos(TagStr, StringArr[i]) > 0) and (Pos(OldStr, StringArr[i]) > 0) ) then
//replace OldStr with NewStr
StringChange(StringArr[i], OldStr, NewStr);
end;
///////////////////////////////////////////////////////////
//// Search ORA- Error function
//// the script exec result, for example : Error or ORA-xxxxx
///////////////////////////////////////////////////////////
function Check_Exec_Script_Result(var ErrStr, LogFile: String): Boolean;
var
LogFileLines: TArrayOfString;
ResultCode: Integer;
i: Integer;
begin
//assign sql file
//load strings and store to SqlFileLines
LoadStringsFromFile(LogFile, LogFileLines);
result := false;
for i:= 0 to GetArrayLength(LogFileLines)-1 do
// if TagStr and OldStr are in the same line
if ( (LogFileLines[i] <> '') and (Pos(ErrStr, LogFileLines[i]) > 0) ) then
MsgBox('Err' + LogFileLines[i] , mbError, MB_OK);
end;
///////////////////////////////////////////////////////////
//// execute Script with Sqlplus
///////////////////////////////////////////////////////////
procedure Exec_Script_Sqlplus(var dbTns, dbUser, dbPwd, scriptPath, batFileName: String);
var
batPath: String;
tmpStr: String;
ResultCode: Integer;
LogFileName: String;
ErrStr: String;
begin
// generate bat file
batPath := ExpandConstant('{tmp}'' + batFileName + '.bat');
tmpStr := 'cd '' + ''#13''#10;
SaveStringToFile(batPath, tmpStr, False);
//tmpStr := 'quit | sqlplus ' + dbUser + '/' + dbPwd + '@' + dbTns + ' @' + scriptPath + ''#13''#10;;
tmpStr := 'echo quit | sqlplus ' + dbUser + '/' + dbPwd + '@' + dbTns + ' @' +'"'+ scriptPath +'"'+ ''#13''#10;
SaveStringToFile(batPath, tmpStr, True);
// set log file path
LogFileName := ExpandConstant('E:'123'' + batFileName + '.txt');
//MsgBox(batPath, mbError, MB_OK);
if Exec(batPath, ' > "' + LogFileName + '"', '', 1, ewWaitUntilTerminated, ResultCode) then
begin
// handle success if necessary; ResultCode contains the exit code
if (ResultCode = 0) then begin
ErrStr := 'ORA';
Check_Exec_Script_Result(ErrStr, LogFileName);
// open ScriptLog file
ShellExec('', ExpandConstant(LogFileName),'', '', SW_SHOW, ewNoWait, ErrorCode)
//MsgBox('OK', mbError, MB_OK);
//result := true;
end;
end
else begin
MsgBox('Exec:' + scriptPath + 'fail,please check parameters setting', mbError, MB_OK);
//handle failure if necessary; ResultCode contains the error code
end;
end;
///////////////////////////////////////////////////////////
//// 1. Set Sql Script Parameters ,ex: DB TNS,Account,PWD
//// 2. Call function ModifySql, to modify Sql Script
//// 3. Call function Exec_Script_Sqlplus, to exec Sql Script with sqlplus
///////////////////////////////////////////////////////////
procedure SetSql_ExecSqlScript();
var
SqlFile: String;
OldString: String;
NewString: String;
TagName: String;
batFileName: String;
SqlFileLines: TArrayOfString;
LocalInfoPath: String;
LocalInfoLines: TArrayOfString;
LocalInfoStr: String;
i: Integer;
begin
//assign sql file
//========================start of 1_Sample_system.sql========================
SqlFile := WizardDirValue() + ''SQL'1_Sample_system.sql';
//create bat file
batFileName := '1_Sample_system';
//load strings and store to SqlFileLines
LoadStringsFromFile(SqlFile, SqlFileLines);
//set modify parameters
TagName := 'CREATE USER';
OldString := 'Sample';
NewString := DBAppUser;
ModifySql(TagName, OldString, NewString, SqlFileLines);
TagName := 'GRANT CREATE';
OldString := 'Sample';
NewString := DBAppUser;
ModifySql(TagName, OldString, NewString, SqlFileLines);
TagName := 'GRANT CONNECT';
OldString := 'Sample';
NewString := DBAppUser;
ModifySql(TagName, OldString, NewString, SqlFileLines);
TagName := 'ALTER USER';
OldString := 'Sample';
NewString := DBAppUser;
ModifySql(TagName, OldString, NewString, SqlFileLines);
TagName := 'IDENTIFIED';
OldString := '1234abcd';
NewString := DBAppPwd;
ModifySql(TagName, OldString, NewString, SqlFileLines);
//save modified strings to file
SaveStringsToFile(SqlFile, SqlFileLines, False);
//Exec Script with Sqlplus
Exec_Script_Sqlplus(DBSystemTNS, DBSystemUser, DBSystemPwd, SqlFile , batFileName);
//========================end of 1_Sample_system.sql========================
//========================start of 2_Sample_create_schema.sql========================
SqlFile := WizardDirValue() + ''SQL'2_Sample_create_schema.sql';
//create bat file
batFileName := '2_Sample_create_schema';
//Exec Script with Sqlplus
Exec_Script_Sqlplus(DBAppTNS, DBAppUser, DBAppPwd, SqlFile , batFileName);
//========================end of 2_Sample_create_schema.sql========================
//========================start of 3_Sample_insert_data.sql========================
SqlFile := WizardDirValue() + ''SQL'3_Sample_insert_data.sql';
//create bat file
batFileName := '3_Sample_insert_data';
//Exec Script with Sqlplus
Exec_Script_Sqlplus(DBAppTNS, DBAppUser, DBAppPwd, SqlFile , batFileName);
//========================end of 3_Sample_insert_data.sql========================
//========================start of 4_Sample_drop_schema.sql========================
SqlFile := WizardDirValue() + ''SQL'4_Sample_drop_schema.sql';
//create bat file
batFileName := '4_Sample_drop_schema';
//load strings form file
LoadStringsFromFile(SqlFile, SqlFileLines);
//set modify parameters
TagName := 'DROP USER';
OldString := 'Sample';
NewString := DBAppUser;
ModifySql(TagName, OldString, NewString, SqlFileLines);
//save modified strings to file
SaveStringsToFile(SqlFile, SqlFileLines, False);
//Exec Script with Sqlplus
//Exec_Script_Sqlplus(DBSystemTNS, DBSystemUser, DBSystemPwd, SqlFile , batFileName);
//========================end of 4_Sample_drop_schema.sql========================
end;
这是我使用命令行oracle sqlplus执行SQL脚本的示例代码
2。有四种基本的SQL脚本
- 1 _sample_system (A)。sql -你需要db系统角色来创建用户
- 2 _sample_create_schema (B)。sql -使用新用户创建表