我需要使用脚本任务从我的SQL任务中获取数据到DataTable对象以生成电子邮件。但是当我尝试使用OLEDB适配器填充任务填充数据时,它会生成错误:
OleDbDataAdapter内部错误:无效的行集访问器:Ordinal = 1 Status = UNSUPPORTEDCONVERSION
截图
如上,
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("[email protected]", "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
请帮帮我。
我这样做是为了将结果集从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);
首先,确保Dts.Variables["onErrorList"].Value
包含有效的记录集,它的类型为System.Object
。
另外,我认为OledbDataAdapter
在从SQL读取时有一些限制,因为不支持所有数据类型,即nvarchar(max)
类似的问题