SSIS 中的错误处理
Error Handling in SSIS
我创建了一个 SSIS 包,它从文件夹中获取 XML 文件并检查模式,如果模式失败,包会记录错误并将文件移动到错误文件夹。目前,我已经完成了所有要求,并且工作正常,除了我在执行结束时收到的错误消息。
- 验证 XML 文件
- 我收到的错误消息
- 我收到的错误消息
该软件包按预期工作正常。我怎样才能抑制这个错误信息?
更新#1:
这是我的错误历史记录
这是我的 XML 架构验证任务属性。
建议
问题可能是由 FailPackageOnFailure
和 FailParentOnFailure
属性引起的。单击 Validate XML
任务并在“属性”选项卡中更改这些属性值。在 Control Flow
中,转到属性并更改 MaximumErrorCount
值并使其大于 1
。
您还可以在此 link:
中找到其他有用的信息
使用脚本任务解决方法
将 3 个变量添加到您的包中:
@[User::XmlPath] Type: String, Description: Store the Xml File Path
@[User:XsdPath] Type: String, Description: Store the Xsd File Path
@[User:IsValidated] Type: Boolean, Description: Store the result of Xml validation
添加脚本任务,select XmlPath
和 User:XsdPath
作为只读变量,IsValidated
作为读写变量
- 将脚本语言设置为
Visual Basic
在脚本编辑器中编写以下代码(这是整个脚本任务代码)
#Region "Imports"
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Math
Imports System.Text
Imports System.Xml
Imports System.Xml.Schema
Imports Microsoft.SqlServer.Dts.Runtime
#End Region
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()>
<System.CLSCompliantAttribute(False)>
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
Public Function LoadXml(xmlFilePath As String, xsdFilePath As String) As Boolean
Dim settings As New XmlReaderSettings()
settings.Schemas.Add(Nothing, xsdFilePath)
settings.ValidationType = ValidationType.Schema
Dim errorBuilder As New XmlValidationErrorBuilder()
AddHandler settings.ValidationEventHandler, New ValidationEventHandler(AddressOf errorBuilder.ValidationEventHandler)
Dim reader As XmlReader = XmlReader.Create(xmlFilePath, settings)
' Read the document...
Dim errorsText As String = errorBuilder.GetErrors()
If errorsText IsNot Nothing Then
Return False
Else
Return True
End If
End Function
Public Sub Main()
Dts.Variables("IsValidated").Value = LoadXml(Dts.Variables("XmlPath").Value.ToString, Dts.Variables("XsdPath").Value.ToString)
Dts.TaskResult = ScriptResults.Success
End Sub
End Class
Public Class XmlValidationErrorBuilder
Private _errors As New List(Of ValidationEventArgs)()
Public Sub ValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
If args.Severity = XmlSeverityType.Error Then
_errors.Add(args)
End If
End Sub
Public Function GetErrors() As String
If _errors.Count <> 0 Then
Dim builder As New StringBuilder()
builder.Append("The following ")
builder.Append(_errors.Count.ToString())
builder.AppendLine(" error(s) were found while validating the XML document against the XSD:")
For Each i As ValidationEventArgs In _errors
builder.Append("* ")
builder.AppendLine(i.Message)
Next
Return builder.ToString()
Else
Return Nothing
End If
End Function
End Class
将优先约束与表达式结合使用来处理验证成功和失败的情况
脚本代码参考
- VB.NET validating XML file against XSD file and parsing through the xml
我创建了一个 SSIS 包,它从文件夹中获取 XML 文件并检查模式,如果模式失败,包会记录错误并将文件移动到错误文件夹。目前,我已经完成了所有要求,并且工作正常,除了我在执行结束时收到的错误消息。
- 验证 XML 文件
- 我收到的错误消息
- 我收到的错误消息
该软件包按预期工作正常。我怎样才能抑制这个错误信息?
更新#1:
这是我的错误历史记录
这是我的 XML 架构验证任务属性。
建议
问题可能是由 FailPackageOnFailure
和 FailParentOnFailure
属性引起的。单击 Validate XML
任务并在“属性”选项卡中更改这些属性值。在 Control Flow
中,转到属性并更改 MaximumErrorCount
值并使其大于 1
。
您还可以在此 link:
中找到其他有用的信息使用脚本任务解决方法
将 3 个变量添加到您的包中:
@[User::XmlPath] Type: String, Description: Store the Xml File Path @[User:XsdPath] Type: String, Description: Store the Xsd File Path @[User:IsValidated] Type: Boolean, Description: Store the result of Xml validation
添加脚本任务,select
XmlPath
和User:XsdPath
作为只读变量,IsValidated
作为读写变量- 将脚本语言设置为
Visual Basic
在脚本编辑器中编写以下代码(这是整个脚本任务代码)
#Region "Imports" Imports System Imports System.Collections.Generic Imports System.Data Imports System.Math Imports System.Text Imports System.Xml Imports System.Xml.Schema Imports Microsoft.SqlServer.Dts.Runtime #End Region <Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> <System.CLSCompliantAttribute(False)> Partial Public Class ScriptMain Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase Enum ScriptResults Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure End Enum Public Function LoadXml(xmlFilePath As String, xsdFilePath As String) As Boolean Dim settings As New XmlReaderSettings() settings.Schemas.Add(Nothing, xsdFilePath) settings.ValidationType = ValidationType.Schema Dim errorBuilder As New XmlValidationErrorBuilder() AddHandler settings.ValidationEventHandler, New ValidationEventHandler(AddressOf errorBuilder.ValidationEventHandler) Dim reader As XmlReader = XmlReader.Create(xmlFilePath, settings) ' Read the document... Dim errorsText As String = errorBuilder.GetErrors() If errorsText IsNot Nothing Then Return False Else Return True End If End Function Public Sub Main() Dts.Variables("IsValidated").Value = LoadXml(Dts.Variables("XmlPath").Value.ToString, Dts.Variables("XsdPath").Value.ToString) Dts.TaskResult = ScriptResults.Success End Sub End Class Public Class XmlValidationErrorBuilder Private _errors As New List(Of ValidationEventArgs)() Public Sub ValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs) If args.Severity = XmlSeverityType.Error Then _errors.Add(args) End If End Sub Public Function GetErrors() As String If _errors.Count <> 0 Then Dim builder As New StringBuilder() builder.Append("The following ") builder.Append(_errors.Count.ToString()) builder.AppendLine(" error(s) were found while validating the XML document against the XSD:") For Each i As ValidationEventArgs In _errors builder.Append("* ") builder.AppendLine(i.Message) Next Return builder.ToString() Else Return Nothing End If End Function End Class
将优先约束与表达式结合使用来处理验证成功和失败的情况
脚本代码参考
- VB.NET validating XML file against XSD file and parsing through the xml