我为Web应用程序创建了WIX安装程序。 它获取应用程序使用的MariaDB连接的凭据。 现在,我想让用户有机会在继续安装之前测试凭据。 这样可以防止常见错误并节省时间。
我的问题是,在安装任何MariaDB连接器之前,我找不到测试连接的方法。 尽管我认为这应该是一个普遍的需求,但我找不到任何示例。 有人做过吗?
我解决了我的问题。 这不是一个完美的解决方案。 但这行之有效,可能会帮助其他人:
我用7zip将MariaDB所需的库打包到一个自解压zip文件中。 从UI触发的CustomAction启动可执行文件并提取MariaDB客户端文件:
<Binary Id="mariadb_client_10.1_exe" SourceFile="resources\MariaDB 10.1.exe" /> <CustomAction Id="ExtractMariadbClient" BinaryKey="mariadb_client_10.1_exe" ExeCommand="-o./ -y" Execute="immediate" />
在欢迎对话框之后触发提取:
<Publish Dialog="WelcomeDlg" Control="Next" Event="DoAction" Value="ExtractMariadbClient" Order="1">1</Publish>
这是我的UI,用于收集凭据:
<?xml version="1.0" encoding="utf-8"?> <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> <Fragment> <UI> <Dialog Id="MariaDBDialog" Width="370" Height="270" Title="!(loc.InstallDirDlg_Title)"> <Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="!(loc.WixUINext)" /> <Control Id="Back" Type="PushButton" X="180" Y="243" Width="56" Height="17" Text="!(loc.WixUIBack)" /> <Control Id="Cancel" Type="PushButton" X="304" Y="243" Width="56" Height="17" Cancel="yes" Text="!(loc.WixUICancel)"> <Publish Event="SpawnDialog" Value="CancelDlg">1</Publish> </Control> <Control Id="Description" Type="Text" X="25" Y="23" Width="280" Height="15" Transparent="yes" NoPrefix="yes" Text="!(loc.MariaDBDlgDescription)" /> <Control Id="Title" Type="Text" X="15" Y="6" Width="200" Height="15" Transparent="yes" NoPrefix="yes" Text="!(loc.MariaDBDlgTitle)" /> <Control Id="BannerBitmap" Type="Bitmap" X="0" Y="0" Width="370" Height="44" TabSkip="no" Text="!(loc.InstallDirDlgBannerBitmap)" /> <Control Id="BannerLine" Type="Line" X="0" Y="44" Width="370" Height="0" /> <Control Id="BottomLine" Type="Line" X="0" Y="234" Width="370" Height="0" /> <Control Id="HostLabel" Type="Text" X="20" Y="65" Width="60" Height="18" NoPrefix="yes" Text="Host" /> <Control Id="Host" Type="Edit" X="80" Y="60" Width="260" Height="18" Property="MARIADB_HOSTNAME" Indirect="no" /> <Control Id="PortLabel" Type="Text" X="20" Y="85" Width="60" Height="18" NoPrefix="yes" Text="Port" /> <Control Id="Port" Type="Edit" X="80" Y="80" Width="260" Height="18" Property="MARIADB_PORT" Indirect="no" /> <Control Id="UserLabel" Type="Text" X="20" Y="105" Width="60" Height="18" NoPrefix="yes" Text="User" /> <Control Id="User" Type="Edit" X="80" Y="100" Width="260" Height="18" Property="MARIADB_USER" Indirect="no" /> <Control Id="PasswordLabel" Type="Text" X="20" Y="125" Width="60" Height="18" NoPrefix="yes" Text="Password" /> <Control Id="Password" Type="Edit" X="80" Y="120" Width="260" Height="18" Property="MARIADB_PASSWORD" Indirect="no" Password="yes" /> <Control Id="DatabaseLabel" Type="Text" X="20" Y="145" Width="60" Height="18" NoPrefix="yes" Text="Database" /> <Control Id="Database" Type="Edit" X="80" Y="140" Width="260" Height="18" Property="MARIADB_DATABASE" Indirect="no" /> <Control Type="PushButton" Id="TestConnection" Width="93" Height="17" X="80" Y="243" Text="Test Connection"> <Publish Event="DoAction" Value="TestDatabaseConnection" Order="1">1</Publish> </Control> </Dialog> </UI> </Fragment> </Wix>
有一个按钮可以测试连接。 当用户单击下一步时,脚本也会触发:
<Publish Dialog="MariaDBDialog" Control="Next" Event="DoAction" Value="TestDatabaseConnection" Order="1">1</Publish> <Publish Dialog="MariaDBDialog" Control="Next" Event="NewDialog" Value="NetworkDlg" Order="2">DATABASE_CONNECTION_VALID="1"</Publish>
您看到DATABASE_CONNECTION_VALID必须为1才能继续。
整个验证过程是在另一个VBScript CustomAction中完成的:
<Binary Id="testDatabaseConnection" SourceFile="resources\testDatabaseConnection.vbs" /> <CustomAction Id="TestDatabaseConnection" BinaryKey="testDatabaseConnection" VBScriptCall="testDatabaseConnection" Execute="immediate" Return="ignore">1</CustomAction>
Function testDatabaseConnection() Set objShell = CreateObject("WScript.Shell") dim hostname hostname = Session.Property("MARIADB_HOSTNAME") dim port port = Session.Property("MARIADB_PORT") dim user user = Session.Property("MARIADB_USER") dim password password = Session.Property("MARIADB_PASSWORD") dim database database = Session.Property("MARIADB_DATABASE") dim currDir currDir = Session.Property("CURRENTDIRECTORY") dim command if Len(password) = 0 then command = currDir & "/MariaDB_client_10.1/bin/mysql.exe -h " & hostname & " --port " & port & " -u " & user & " -e ""show databases;""" else command = currDir & "/MariaDB_client_10.1/bin/mysql.exe -h " & hostname & " --port " & port & " -u " & user & " -p" & password & " -e ""show databases;""" end if dim ret Set ret = objShell.Exec(command) execStdOut = ret.StdOut.ReadAll() execStdErr = ret.StdErr.ReadAll() if (instr(1, execStdOut, vbCrLf & database & vbCrLf) > 0) then MsgBox "MariaDB ist erreichbar und die Datenbank " & database & " ist vorhanden.", vbInformation, "Verbindungstest" Session.Property("DATABASE_CONNECTION_VALID") = "1" elseIf (Len(execStdErr) > 0) then MsgBox "Verbindung zur MariaDB konnte nicht hergestellt werden. Fehlermeldung: " & execStdErr, vbCritical, "Verbindungstest" Session.Property("DATABASE_CONNECTION_VALID") = "0" else MsgBox "Verbindung zur MariaDB erfolgreich, aber Datenbank ist nicht vorhanden. Folgende Datenbanken existieren:" & vbCrLf & execStdOut, vbCritical, "Verbindungstest" Session.Property("DATABASE_CONNECTION_VALID") = "0" end if End Function
希望它可以帮助其他人并节省一些时间。