使用C#从完整结果集变量获取数据到脚本任务

时间:2019-03-04 04:26:12

标签: c# variables ssis oledb script-task

我需要使用脚本任务将数据从SQL任务中的数据获取到DataTable对象,以生成电子邮件。但是,当我尝试使用OLEDB适配器填充任务填充数据时,会生成错误:

  

OleDbDataAdapter内部错误:无效的行集访问器:Ordinal = 1 Status = UNSUPPORTEDCONVERSION

屏幕截图

enter image description here

如上所述

public void Main()
{
    // TODO: Add your code here
    DataTable dt = new DataTable();
    String message = "";
    OleDbDataAdapter adapter = new OleDbDataAdapter();

    if (Dts.Variables.Contains("onErrorList") == true)
    {  
        try
        {
            try
            {
                adapter.Fill(dt, Dts.Variables["onErrorList"].Value);
            } catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            foreach (DataRow row in dt.Rows)
            {
                message = message + "\n" + "Error Time : " + row["message_time"] + "\n" + "Execution Path : " + row["executionpath"] + "\n" + "Error : " + row["MESSAGE"];
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }

    message = Dts.Variables["executionMessage"].Value + "\n" + message;


    try {
        sendMail("umairr.ayra@gmail.com", "Error in  ETL ", message);
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    catch (Exception e)
    {

        MessageBox.Show(e.Message, "Mail Sending Failed");
        Dts.TaskResult = (int)ScriptResults.Failure;
    }


}

#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
/// 
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
    Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
    Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion

此行是生成错误的地方:

adapter.Fill(dt, Dts.Variables["onErrorList"].Value); 

和我用来获取值的SQL代码

    SELECT  message_time,CAST(execution_path AS NVARCHAR(100)) AS executionpath , MESSAGE
FROM   (
       SELECT  em.*
       FROM    SSISDB.catalog.event_messages AS em 
       WHERE   em.operation_id = (SELECT MAX(execution_id) FROM SSISDB.catalog.executions)
          -- AND event_name NOT LIKE '%Validate%'
       )q 
WHERE   event_name = 'OnError'
ORDER BY message_time DESC

结果集到变量的映射 mapping to object variable

变量类型 Variable list in my package

请帮助我。

2 个答案:

答案 0 :(得分:1)

我这样做是为了将结果集从SSIS变量导入到数据表中,以便以后在我的代码中使用。这就是我的做法。

您的SSIS变量必须是对象数据类型。如果不是,则需要先使用它。

然后,您使用c#来获取数据并将其转换为如下所示的适配器(而不是像您尝试的那样直接转换为适配器):

// import SSIS variable of object type
Object OBJDataTableValidFieldListFull = Dts.Variables["User::SSISVariableObject"].Value;

// create datatable variable and dataadpapter
DataTable dtValidFieldListFull = new DataTable();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();

// fill datatable from variable passed from SSIS Object type
dataAdapter.Fill(dtValidFieldListFull, OBJDataTableValidFieldListFull);

答案 1 :(得分:0)

首先,请确保Dts.Variables["onErrorList"].Value包含有效记录集,并且其类型为System.Object

此外,我认为OledbDataAdapter在读取SQL时有一些限制,因为并非所有数据类型都受支持,即nvarchar(max)

类似问题