我想编译一个设置,该设置将使用用户提供的凭据连接到远程数据库,然后使用 .sql 脚本安装几个 db 组件。
使用 Inno Setup 可以吗?
更多细节:
我想要一个自定义表单,要求用户输入数据库地址和凭据,然后运行一个命令,该命令将执行一个更新远程数据库服务器的 sql 脚本。
如果更新成功 - 成功完成安装。
这是一个相当普遍的问题——我有很多定制的设置应该连接到不同的服务器/运行不同的脚本——这个想法是构建一个通用的表单来提供这个功能。
我想编译一个设置,该设置将使用用户提供的凭据连接到远程数据库,然后使用 .sql 脚本安装几个 db 组件。
使用 Inno Setup 可以吗?
更多细节:
我想要一个自定义表单,要求用户输入数据库地址和凭据,然后运行一个命令,该命令将执行一个更新远程数据库服务器的 sql 脚本。
如果更新成功 - 成功完成安装。
这是一个相当普遍的问题——我有很多定制的设置应该连接到不同的服务器/运行不同的脚本——这个想法是构建一个通用的表单来提供这个功能。
我不认为你可以有一个完全通用的形式,因为对于不同的服务器,你可能需要一个连接字符串,或者一个服务器名称和一个(可选的)端口;对于某些服务器,您将使用系统身份验证,对于其他服务器,您将使用用户名密码元组。
话虽如此,我会给你一个小的演示 Inno 脚本,它询问服务器名称和端口、用户名和密码,然后进行一些测试,然后执行一个应用程序,该应用程序被提取(通过代码)到 temp 目录并将被删除由安装程序。您可以将此作为脚本的起点。拥有一些这样的片段,并根据需要将它们包含在您的脚本中可能就是您所需要的:
[Setup]
AppID=DBUpdateTest
AppName=Test
AppVerName=Test 0.1
AppPublisher=My Company, Inc.
DefaultDirName={pf}\Test
DefaultGroupName=Test
DisableDirPage=yes
DisableProgramGroupPage=yes
OutputBaseFilename=setup
PrivilegesRequired=none
[Files]
Source: "isql.exe"; DestDir: "{tmp}"; Flags: dontcopy
Source: "update_V42.sql"; DestDir: "{tmp}"; Flags: dontcopy
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Code]
var
DBPage: TInputQueryWizardPage;
procedure InitializeWizard;
begin
DBPage := CreateInputQueryPage(wpReady,
'Database Connection Information', 'Which database is to be updated?',
'Please specify the server and the connection credentials, then click Next.');
DBPage.Add('Server:', False);
DBPage.Add('Port:', False);
DBPage.Add('User name:', False);
DBPage.Add('Password:', True);
DBPage.Values[0] := GetPreviousData('Server', '');
DBPage.Values[1] := GetPreviousData('Port', '');
DBPage.Values[2] := GetPreviousData('UserName', '');
DBPage.Values[3] := GetPreviousData('Password', '');
end;
procedure RegisterPreviousData(PreviousDataKey: Integer);
begin
SetPreviousData(PreviousDataKey, 'Server', DBPage.Values[0]);
SetPreviousData(PreviousDataKey, 'Port', DBPage.Values[1]);
SetPreviousData(PreviousDataKey, 'UserName', DBPage.Values[2]);
SetPreviousData(PreviousDataKey, 'Password', DBPage.Values[3]);
end;
function NextButtonClick(CurPageID: Integer): Boolean;
var
ResultCode: Integer;
begin
Result := True;
if CurPageID = DBPage.ID then begin
if DBPage.Values[0] = '' then begin
MsgBox('You must enter the server name or address.', mbError, MB_OK);
Result := False;
end else if DBPage.Values[2] = '' then begin
MsgBox('You must enter the user name.', mbError, MB_OK);
Result := False;
end else if DBPage.Values[3] = '' then begin
MsgBox('You must enter the user password.', mbError, MB_OK);
Result := False;
end else begin
ExtractTemporaryFile('isql.exe');
ExtractTemporaryFile('update_V42.sql');
if Exec(ExpandConstant('{tmp}') + '\isql.exe', '--user ' + DBPage.Values[2]
+ ' --password ' + DBPage.Values[3] + ' --database ' + DBPage.Values[0]
+ ':foo --script update_V42.sql', '',
SW_HIDE, ewWaitUntilTerminated, ResultCode)
then begin
// check ResultCode and set Result accordingly
Result := ResultCode = 0;
end else begin
MsgBox('Database update failed:'#10#10 + SysErrorMessage(ResultCode),
mbError, MB_OK);
Result := False;
end;
end;
end;
end;
当心:我还没有完全测试这个,所以可能需要更多的代码来正确清理所有内容。绝对缺少错误处理!