2013-01-16 18 views
0

我有一个SSIS包,它有一个FOREACH循环容器,并且在循环中有一个脚本任务。在脚本任务的代码,作为以下给出SSIS脚本任务中的OleDbCommand.ExecuteNonQuery()函数中的未指定错误

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”)。在的每次迭代对于每个容器价值的变化。它看起来像 -

"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的数据表时会引发问题。

任何解决方案,以避免这种情况?

+1

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

回答

0

我相信你缺少的是告诉参数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中应该是一样的减去分号。

+0

Thanks @billinkc。我想我能在之前的练习中用SQLConnection完成任务。但在这里我必须使用OleDbconnection本身,以便将对象Source_Data中的数据填充到数据表(使用oleDA.Fill方法)。当我使用oleDbConnection时,发生数据表的问题。 – user1668795

0

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

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

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

我改变了我的OLEDB连接到ADO.Net连接,它工作正常。