【问题标题】:index was out of bounds error in vb.net scriptvb.net 脚本中的索引超出范围错误
【发布时间】:2012-01-20 19:56:18
【问题描述】:

我遇到以下脚本的索引超出范围错误。我有 1 个输入列和 11 个输出列添加到 SSIS 脚本组件。它们的数据类型都是字符串。不知道我哪里出错了。 提前致谢。

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
    Dim strRow As String
    Dim strColSeperator As String
    Dim rowValues As String()
    strRow = Row.Line.ToString()
    If strRow.Contains("-") Then
        strColSeperator = ("-")
    ElseIf strRow.Contains(";") Then
        strColSeperator = ";"
    End If

    rowValues = Row.Line.Split(CChar(strColSeperator))
    Row.Invoices = rowValues.GetValue(0).ToString()
    Row.Detail = rowValues.GetValue(1).ToString()
    Row.Date = rowValues.GetValue(2).ToString()
    Row.Something1 = rowValues.GetValue(3).ToString()
    Row.Something2 = rowValues.GetValue(4).ToString()
    Row.SomeNumber = rowValues.GetValue(5).ToString()
    Row.CustomerName = rowValues.GetValue(6).ToString()
    Row.InvoiceNumber = rowValues.GetValue(7).ToString()
    Row.InvoiceNumber2 = rowValues.GetValue(8).ToString()
    Row.InvoiceNumber3 = rowValues.GetValue(9).ToString()
    Row.InvoiceNumber4 = rowValues.GetValue(10).ToString()

End Sub

【问题讨论】:

  • 美元到饺子你那里有臭鼬数据。在您的脚本转换中,添加第二个输出(输出 1)以捕获有问题的行。在 Output 1 中添加一列,列名 Line 与现有数据类型匹配。将上面的代码包装在 try/catch 块中。在 catch 部分,添加这两行 Output1Buffer.AddRow()Output1Buffer.Line = Row.Line 将它们定向到文件中或添加数据查看器,看看为什么它无法拆分为预期的大小。

标签: vb.net ssis script-component


【解决方案1】:
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer) 
    Dim strRow As String 
    Dim strColSeperator As Char ' <== Char instead of string 
    Dim rowValues As String() 

    strRow = Row.Line.ToString() 
    If strRow.Contains("-") Then 
        strColSeperator = "-"c ' <== the 'c' denotes a Char literal
    ElseIf strRow.Contains(";") Then 
        strColSeperator = ";"c 
    End If 

    rowValues = strRow.Split(strColSeperator) ' <== Now CChar is obsolete
    MsgBox("rowValues length = " & rowValues.Length) ' <== Check to see if it's really as long as expected!
    Row.Invoices = rowValues(0) ' <== Use the array indexer instead of GetValue and ToString
    Row.Detail = rowValues(1) 
    Row.Date = rowValues(2) 
    Row.Something1 = rowValues(3)
    Row.Something2 = rowValues(4)
    Row.SomeNumber = rowValues(5)
    Row.CustomerName = rowValues(6)
    Row.InvoiceNumber = rowValues(7)
    Row.InvoiceNumber2 = rowValues(8)
    Row.InvoiceNumber3 = rowValues(9)
    Row.InvoiceNumber4 = rowValues(10)
End Sub 

【讨论】:

  • 奥利弗,我运行了你提供的代码。它会持续一段时间,在消息框上主要显示 11 和 12(大约 14 次)。在 14 次点击之后是 7 次,之后它再次失败并出现同样的问题。我检查了我的数据,前 20 行左右的数据相同。知道会发生什么吗?
  • 尝试显示strRow,以查看它真正包含的内容。我的猜测是您使用了错误的分隔符。如果分隔符是“;”怎么办并且您的行包含带有减号(“-”)的负值?
猜你喜欢
  • 1970-01-01
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多