无法将类型为'system .__ comobject'的com对象强制转换为类类型'system.data.sqlclient.sqlconnection

无法将类型为'system .__ comobject'的com对象强制转换为类类型'system.data.sqlclient.sqlconnection

问题描述:

您好我的SSIS包中的数据流中的脚本组件中有以下代码/脚本。

但是当我尝试执行它时,它会显示上面的错误消息作为标题。

以下是我的代码:



Hi I have the following codes/scripts in my Script Component in data flow in my SSIS package.
However when I try to execute it, it shows me the above error message as the title.
Below are my codes:

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Data.SqlClient;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
    IDTSConnectionManager100 connMgr;
    SqlConnection sqlConn;
    SqlDataReader sqlReader;

    public override void AcquireConnections(object Transaction)
    {
        connMgr = this.Connections.GsfConnection;
        sqlConn = (SqlConnection)connMgr.AcquireConnection(null); //error should happen here
    }

    public override void PreExecute()
    {
        base.PreExecute();
        SqlCommand cmd = new SqlCommand("EXEC usp_SSISPartNumber_Select " + "725", sqlConn);
        sqlReader = cmd.ExecuteReader();
    }

    public override void CreateNewOutputRows()
    {
    }

    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        while (sqlReader.Read())
        {
            {
                Output0Buffer.AddRow();
                Output0Buffer.NumberFamily = sqlReader.GetString(0);
                Output0Buffer.NumberInternal = sqlReader.GetString(1);
                Output0Buffer.ItemHeaderId = sqlReader.GetInt32(2);
            }
        }
    }

    public override void PostExecute()
    {
        base.PostExecute();
        sqlReader.Close();
    }

    public override void ReleaseConnections()
    {
        connMgr.ReleaseConnection(sqlConn);
    }  
}





我知道问题发生在AcquireConnection方法,但我还没有关于如何解决它的想法。任何帮助,将不胜感激。谢谢。



I know that the problem happens at the "AcquireConnection" method, but I have not idea on how to solve it. Any help would be appreciated. Thank you.

我们无法帮助您 - 我们不知道您的manager.AcquireConnection将返回什么数据类型。

因此,请查看方法的intellisense,并查看它返回的类型。如果它是某种描述的泛型类型,则首先更改代码:

We can't help you - we have no idea what datatype your manager.AcquireConnection is going to return.
So look at the intellisense for the method, and see what type it returns. If it's a generic type of some description then start by changing the code:
object o = connMgr.AcquireConnection(null);

并在其上放置一个断点。运行代码,使用单步执行该行,并查看o中的值的类型。



我们不能为你做任何事情!

And put a breakpoint on it. Run your code, Execute the line using single step, and look at what type of value is in "o".

Be we can't do any of that for you!