0

我有一个 SSIS 包,它有一个 FOREACH 循环容器,在循环内我有一个脚本任务。脚本任务中的代码如下

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Configuration

<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
    Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Public Sub Main()
Try
            Dim oleConn As New OleDbConnection()
            Dim strConn As String = Dts.Variables("ConfiguredValue").Value.ToString()
            oleConn.ConnectionString = strConn
            Dim oleDA As New OleDbDataAdapter()
            Dim dt As New DataTable()
            oleDA.Fill(dt, Dts.Variables("Source_Data").Value)
            oleConn.Open()
            Dim oleComm As New OleDbCommand("Insert_Into_Destination")
            oleComm.CommandType = CommandType.StoredProcedure
            oleComm.Connection = oleConn
            oleComm.Parameters.AddWithValue("@tvp", dt)
            oleDA.InsertCommand = oleComm
            oleComm.ExecuteNonQuery()
            oleConn.Close()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class

变量 Dts.Variables("ConfiguredValue").Value 的值在 For Each 容器的每次迭代中发生变化。它看起来像 -

"Data Source=server_name;Initial Catalog=db_name;User ID=user_id;Password = Password;Provider=SQLOLEDB.1; Persist Security Info=True;Integrated Security=SSPI;Auto Translate=False;"

问题是 - 执行包时,在 ExecuteNonQuery() 处抛出异常,显示“未指定错误”。

当我尝试类似的代码插入一个整数值时没有错误。当它是我作为输入参数传递给 SP 的数据表时会导致问题。

有什么办法可以避免这种情况吗?

4

2 回答 2

0

我相信您缺少的是告诉参数它应该是什么 TVP 以及它的类型(最后 2 行)。我的表值参数在 C# 中,但同样的概念也适用。

    command = new System.Data.SqlClient.SqlCommand("TwitterAdd");
    command.CommandType = System.Data.CommandType.StoredProcedure;
    command.Connection = connection;

    // Assigning a table valued parameter looks much like any other parameter
    System.Data.SqlClient.SqlParameter tvp = command.Parameters.AddWithValue("@tvp", dataTable);

    // this is the only special sauce
    tvp.SqlDbType = System.Data.SqlDbType.Structured;
    tvp.TypeName = "dbo.MY_TABLE_VALUED_PARAMETER";

这在 VB 中应该是相同的减去分号。

于 2013-01-16T14:32:43.857 回答
0

我遇到了同样的问题。我收到的错误消息是:“由于符号不匹配或数据溢出以外的原因,无法转换命令参数 [0] ''数据值。”

在上面引用的 MSDN 文章中,我看不到对 OLE DB 的任何引用 - TVP 似乎只是 SqlClient。

此外,在System.Data.OleDB.OleDbType枚举中没有结构化选项。

我将我的 OLEDB 连接更改为 ADO.Net Connection,它工作正常。

于 2014-05-01T16:04:28.503 回答