日期:2014-05-18  浏览次数:20665 次

SSIS 数据流中脚本组件应用问题
/* Microsoft SQL Server Integration Services Script Component
* Write scripts using Microsoft Visual C# 2008.
* ScriptMain is the entry point class of the script.*/

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

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
  string st = string.Empty;
  SqlConnection cnn;
  IDTSConnectionManager100 cnManager;
  SqlCommand cmd = new SqlCommand();
   
  public override void PreExecute()
  {
  base.PreExecute();
  //get conn;
  cnManager = base.Connections.连接;
  cnn = (SqlConnection)cnManager.AcquireConnection(null);
   
  }

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

  public override void 输入0_ProcessInputRow(输入0Buffer Row)
  {
  cnManager = base.Connections.连接;
  cnn = (SqlConnection)cnManager.AcquireConnection(null);
   
  string sqlstring = "select distinct ipid from p_ip where '" + Row.UserIp + "' between start_t and end_t";
  DataTable dt = new DataTable();
  cmd.Connection = cnn;
  cmd.CommandText = sqlstring;
  cmd.CommandType = CommandType.Text;

  SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  adapter.Fill(dt);

  if (dt.Rows.Count == 0)
  Row.ipid = -100;
  else
  Row.ipid = int.Parse(dt.Rows[0]["ipid"].ToString());
   
   
   


  }
}

报错:无法将类型为“System.__ComObject”的 COM 对象强制转换为类类型“System.Data.SqlClient.SqlConnection”。表示 COM 组件的类型实例不能强制转换为不表示 COM 组件的类型;不过,只要基础 COM 组件支持对接口 IID 的 QueryInterface 调用,就能将这些实例强制转换为接口。

高手帮忙解释下,小弟不胜感激!


------解决方案--------------------
这里的脚本,有很多限制的,很多东西都不可用,我一般只是用来处理一些字符什么的,

同样期待高手
------解决方案--------------------
看下 base.Connections.连接 这个连接你所配的类型能不能转成System.Data.SqlClient.SqlConnection 不是所有的都可以转的
------解决方案--------------------
试试这个行不行
C# code

...
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
    private SqlConnection sqlCon;
    private SqlCommand sqlCmd;
    private SqlDataReader sqlReader;

    public override void AcquireConnections(object Transaction)
    {
        sqlCon = new SqlConnection("这里替换成sqlserver连接字符串");
        sqlCmd = new SqlCommand();
        sqlCon.Open();
        sqlCmd.Connection = sqlCon;
    }

    public override void PreExecute()
    {
        base.PreExecute();
        /*
          Add your code here for preprocessing or remove if not needed
        */
        
    }

    public override void PostExecute()
    {
        base.PostExecute();
        /*
          Add your code here for postprocessing or remove if not needed
          You can set read/write variables here, for example:
          Variables.MyIntVar = 100
        */
        sqlCon.Close();
    }

    public override void source_ProcessInputRow(sourceBuffer Row)
    {
        /*
          Add your code here
        */
        
    }
}