使用 C# 将完整结果集变量中的数据获取到脚本任务
Get data from a full result set variable in to script task using C#
我需要使用脚本任务将数据从我的 SQL 任务获取到 DataTable 对象以生成电子邮件。但是当我尝试使用 OLEDB 适配器填充任务填充数据时,它会生成错误:
OleDbDataAdapter Internal error: invalid row set accessor: 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("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
结果集映射到变量
变量类型
请帮我解决这个问题。
首先,确保 Dts.Variables["onErrorList"].Value
包含一个有效的记录集并且它是 System.Object
.
类型
此外,我认为 OledbDataAdapter
在读取 SQL 时有一些限制,因为并非所有数据类型都受支持,即 nvarchar(max)
类似问题
我这样做是为了将结果集从 SSIS 变量导入数据 table,以便稍后在我的代码中使用。我就是这样做的。
您的 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);
我需要使用脚本任务将数据从我的 SQL 任务获取到 DataTable 对象以生成电子邮件。但是当我尝试使用 OLEDB 适配器填充任务填充数据时,它会生成错误:
OleDbDataAdapter Internal error: invalid row set accessor: 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("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
结果集映射到变量
变量类型
请帮我解决这个问题。
首先,确保 Dts.Variables["onErrorList"].Value
包含一个有效的记录集并且它是 System.Object
.
此外,我认为 OledbDataAdapter
在读取 SQL 时有一些限制,因为并非所有数据类型都受支持,即 nvarchar(max)
类似问题
我这样做是为了将结果集从 SSIS 变量导入数据 table,以便稍后在我的代码中使用。我就是这样做的。
您的 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);