执行SQL查询时Wix错误-2147217900

时间:2016-06-29 13:09:41

标签: sql-server wix database-project

我正在使用WIX 3.10构建一个安装项目。我需要从安装程序创建一个SQL Server数据库,因为我使用的是Visual Studio数据库项目生成的脚本。

我的WIX代码是:

events: [{
  title:"My repeating event",
  start: '10:00', // a start time (10am in this example)
  end: '14:00', // an end time (6pm in this example)
  //Days of week. an array of zero-based day of week integers (0=Sunday)
  dow: [ 1, 4 ] // Repeat monday and thursday
}],

<Binary Id="SQLCreateScript" SuppressModularization="no"  SourceFile="$(var.TestInstallDBProject.TargetDir)$(var.TestInstallDBProject.ProjectName)_Create.sql" />

它抛出了这个错误:

it throws following Error while installation

我打开了在Notepad ++中生成的文件但看不到字符。错误讯息

File In Notepad++

1 个答案:

答案 0 :(得分:0)

要解决我的问题,我使用自定义MSbuild任务,从文本(SQL)文件中删除BOM信息。这是代码:

using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace CustomBuildTasks
{
    public class RemoveBOMInfo : Task
    {
        private  ITaskItem[] _FilesToRemoveBOMInfo;

        [Required]
        public  ITaskItem[] FilesToRemoveBOMInfo { get { return _FilesToRemoveBOMInfo; } set { _FilesToRemoveBOMInfo = value; } } 

        public override bool Execute()
        {
            if (FilesToRemoveBOMInfo == null || FilesToRemoveBOMInfo.Count() < 1)
            {
                Log.LogError("FilesToRemoveBOMInfo can't be null or Empty.");
                return false; 
            }
            //var Files = FilesToRemoveBOMInfo.Split(';');
            foreach (var File in FilesToRemoveBOMInfo) 
            {
                var str = File.GetMetadata("Identity");
                if (!System.IO.File.Exists(str)) 
                {
                    Log.LogError("File {0} not found.", str);
                    continue;
                }
                using (var file = System.IO.File.Open(str, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    int fByte = file.ReadByte();
                    if (fByte != 239)
                        continue;
                    fByte = file.ReadByte();
                    if (fByte != 187)
                        continue;
                    fByte = file.ReadByte();
                    if (fByte != 191)
                        continue;
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                    {
                        for (int i = 0; i > -1; ) 
                        {
                            i = file.ReadByte();
                            if (i > 0)
                            {
                                ms.WriteByte((byte)i);
                            }
                        }
                        file.Close();
                        System.IO.File.Delete(str);
                        using (var oFile = System.IO.File.Create(str))
                        {
                            ms.WriteTo(oFile);
                            oFile.Close();
                        }
                    }
                }
            }
            return true;
        }
    }
}

并在MSBuild脚本中用作:

<Target Name="AfterResolveReferences" >
    <RemoveBOMInfo FilesToRemoveBOMInfo="%(_ResolvedProjectReferencePaths.RelativeDir)%(_ResolvedProjectReferencePaths.Name)_Create.sql" Condition="'%(_ResolvedProjectReferencePaths.IsSQLProj)' == 'True'" ></RemoveBOMInfo>    
</Target>