【问题标题】:Unspecified Error in OleDbCommand.ExecuteNonQuery() function within SSIS script taskSSIS 脚本任务中 OleDbCommand.ExecuteNonQuery() 函数中的未指定错误
【发布时间】:2013-01-16 08:37:14
【问题描述】:

我有一个 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 的数据表时会导致问题。

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

【问题讨论】:

  • 您不能将DataTable 传递给存储过程。您需要使用表值参数(TVP) 或xml。查看此MSDN 链接http://msdn.microsoft.com/en-us/library/bb675163.aspx

标签: vb.net ssis


【解决方案1】:

我相信您缺少的是告诉参数它应该是什么 TVP 以及它的类型(最后 2 行)。我的table valued parameter 在 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中应该是相同的减去分号。

【讨论】:

  • 谢谢@billinkc。我想我可以在之前的练习中使用 SQLConnection 完成这项任务。但是这里我必须使用 OleDbconnection 本身,以便将对象 Source_Data 中的数据填充到数据表中(使用 oleDA.Fill 方法)。当我使用 oleDbConnection 时,我的数据表出现问题。
【解决方案2】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多