You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WIX工具集安装包连接SQL数据库(-2147467259 ASTORRULES)错误排查

问题背景

我正在为Advance Steel(AS)软件开发一款配套工具集,包含DLL文件与图片。安装该工具集时,需完成以下操作:

  • 将文件复制到用户选择的两个AS软件安装目录
  • 向AS的ASTORRULES数据库写入SQL数据
  • 生成XML文件

我使用WIX工具集编写了安装包项目代码,但运行安装程序时出现错误:

Failed to connect to SQL database (-2147467259 ASTORRULES)

我的WIX代码

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:util="http://schemas.microsoft.com/wix/UtilExtension"
    xmlns:sql="http://schemas.microsoft.com/wix/SqlExtension">
    <?define MyWpfApplication_TargetDir=$(var.MyWpfApplication.TargetDir)?>
    <?define Win64 = "yes" ?>
    <?define SqlInstanceName = "YourInstanceNameHere" ?>
    <?define WixExtension ="WixSqlExtension" ?>
  <Product Id="17a4f85a-1219-4143-a0d6-e9ebc6084064" Name="My Wpf Application" Language="1033" Version="1.0.0.0" Manufacturer="Mesta-Automation.com" UpgradeCode="3936e1bc-c06c-45ae-b1db-430cf31dd48b">
    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
    
    <Icon Id="icon.ico" SourceFile="$(var.ProjectDir)Icon.ico" />
    <Property Id="ARPPRODUCTICON" Value="icon.ico" />

    <WixVariable Id="WixUIBannerBmp" Value="Images\installer_top-banner.bmp" />
    <WixVariable Id="WixUIDialogBmp" Value="Images\installer_background.bmp" />
    <WixVariable Id="WixUILicenseRtf" Value="$(var.ProjectDir)\license.rtf" />

    <Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER" />
    <UIRef Id="WixUI_InstallDir" />


    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate EmbedCab="yes" />

    <Feature Id="ProductFeature" Title="My Wpf Application" Level="1">
      <ComponentGroupRef Id="ProductComponents" />
      <ComponentRef Id="ApplicationShortcut" />
      <ComponentRef Id="ApplicationShortcutDesktop" />

        <!--
         <ComponentGroupRef Id="Files_files" /> -->
    </Feature>

      <Binary Id="CreateUpsizingDatabase" SourceFile="FILECAIDAT\dbo.RULE_HRLDefinition.sql" />
      <util:User Id="SQLUser" Name="ADMINISTRATOR\CANHQUY" />
      <sql:SqlDatabase Id="SqlDatabase" Database="ASTORRULES" Server="(LocalDB)\AdvanceSteel2022" User="SQLUser" />

  </Product>

  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="MyWpfApplication">
          <Directory Id="Files" Name="Files" />
        </Directory>
      </Directory>
      <Directory Id="ProgramMenuFolder">
        <Directory Id="ApplicationProgramsFolder" Name="MyWpfApplication" />
      </Directory>
      <Directory Id="DesktopFolder" Name="Desktop" />
    </Directory>
  </Fragment>
    <Fragment>
    <DirectoryRef Id="ApplicationProgramsFolder">
      <Component Id="ApplicationShortcut" Guid="9bd13330-6540-406f-a3a8-d7f7c69ae7f9">
        <Shortcut Id="ApplicationStartMenuShortcut" Name="MyWpfApplication" Description="MyWpfApplication" Target="[INSTALLFOLDER]MyWpfApplication.exe" WorkingDirectory="INSTALLFOLDER" />
        <RemoveFolder Id="RemoveApplicationProgramsFolder" Directory="ApplicationProgramsFolder" On="uninstall" />
        <RegistryValue Root="HKCU" Key="Software\MyWpfApplication" Name="installed" Type="integer" Value="1" KeyPath="yes" />
      </Component>
    </DirectoryRef>
    <DirectoryRef Id="DesktopFolder">
      <Component Id="ApplicationShortcutDesktop" Guid="cde1e030-eb64-49a5-b7b8-400b379c2d1a">
        <Shortcut Id="ApplicationDesktopShortcut" Name="MyWpfApplication" Description="MyWpfApplication" Target="[INSTALLFOLDER]MyWpfApplication.exe" WorkingDirectory="INSTALLFOLDER" />
        <RemoveFolder Id="RemoveDesktopFolder" Directory="DesktopFolder" On="uninstall" />
        <RegistryValue Root="HKCU" Key="Software\MyWpfApplication" Name="installed" Type="integer" Value="1" KeyPath="yes" />
      </Component>
    </DirectoryRef>
  </Fragment>
 
  <Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
> TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->
      <!-- <Component Id="ProductComponent"> -->
> TODO: Insert files, registry keys, and other resources here. -->
      <!-- </Component> -->
      <Component Id="file_ASNetJoints.dll" Guid="0f9a1acd-803f-4ecb-a283-7537a05a5eb1">
        <File Id="file_ASNetJoints.dll" Name="ASNetJoints.dll" Source="FILECAIDAT\ASNetJoints.dll" />
      </Component>
        <Component Id="SqlComponent" Guid="15CCE46E-8EA5-42CA-80C5-AC3DB30A9716">
            <sql:SqlScript Id="CreateDatabases" SqlDb="SqlDatabase" ExecuteOnInstall="yes" BinaryKey="CreateUpsizingDatabase" />
            <CreateFolder/>
        </Component>
    </ComponentGroup>
    
  </Fragment>

    <Fragment>
        <!-- Định nghĩa Binary cho file SQL -->
        <Binary Id="SqlScriptBinary" SourceFile="FILECAIDAT\dbo.RULE_HRLDefinition.sql" />

        <CustomAction Id="ChangeSqlScript"
BinaryKey="Installer.CA"
DllEntry="ChangeSqlScript"
Execute="immediate"
Return="check"
Impersonate='yes'/>
        <InstallExecuteSequence>
            <Custom Action="ChangeSqlScript" After="InstallFinalize">NOT Installed AND VersionNT</Custom>
        </InstallExecuteSequence>
    </Fragment>

</Wix>
错误排查与修复方案

1. SQL身份验证配置错误

SQL LocalDB默认使用Windows集成身份验证,你显式指定ADMINISTRATOR\CANHQUY用户会导致身份验证失败。修复方式:

  • 删除<util:User Id="SQLUser" Name="ADMINISTRATOR\CANHQUY" />节点
  • 修改SqlDatabase元素,移除User属性,让安装程序使用当前管理员身份连接:
<sql:SqlDatabase Id="SqlDatabase" Database="ASTORRULES" Server="(LocalDB)\AdvanceSteel2022" />

2. 错误码对应问题分析

错误码-2147467259对应E_FAIL,常见触发原因:

  • LocalDB实例AdvanceSteel2022未启动:手动执行sqllocaldb start AdvanceSteel2022启动实例后再测试安装
  • 当前用户无数据库访问权限:确保安装包以管理员身份运行(右键选择「以管理员身份运行」)
  • ASTORRULES数据库不存在:提前确认AS软件已正确安装并创建该数据库

3. 冗余SQL脚本定义

你重复定义了两个指向同一SQL文件的Binary节点:CreateUpsizingDatabase和SqlScriptBinary,保留其中一个即可,避免逻辑混淆。

4. 自定义动作逻辑问题

ChangeSqlScript自定义动作设置在InstallFinalize之后执行,此时SQL脚本已经执行完毕,无法达到修改脚本的目的。调整方案:

  • 将动作移至SqlScript执行之前,比如After="InstallInitialize"
  • 修改执行条件为NOT Installed AND VersionNT >= 601(明确适配Windows 7及以上系统)
  • 如果涉及文件修改,将Execute属性改为deferred并关闭Impersonate

5. 无效注释代码

代码中存在格式错误的注释行,会导致XML解析异常:

> TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->
> TODO: Insert files, registry keys, and other resources here. -->

修改为标准XML注释格式:

<!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->
<!-- TODO: Insert files, registry keys, and other resources here. -->

内容的提问来源于stack exchange,提问作者lecanhquy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 19:24:57