将 OLEDB 行集转换为队列对象,以便进行多线程和锁定



我有一个Execute SQL Task,它使用用于并行驱动其他子进程Full Result Set返回数据表。问题是需要锁定Object因此每个锁只处理一次。如何访问 C# 脚本任务中的 Object 变量并转换为队列数据类型以进行锁定?

Execute SQL Task将结果存储在 User::CoreTables 中,然后我想每次都获取第一行的值:

// The line that fails to do the conversion, not sure how to do this
System.Collections.Generic.Queue<string> tablesQueue = (System.Collections.Generic.Queue<string>)Dts.Variables["User::CoreTables"].Value;
lock (tablesQueue)
{
    //If number of rows in queue is greater than 0 then dequeue row for processing
    //and set DoWork to true. Otherwise set DoWork to false as there is nothing to process
    if (tablesQueue.Count > 0)
    {
        Dts.Variables["User::TableToProcess"].Value = tablesQueue.Dequeue();
        Dts.Variables["User::DoWork"].Value = true;
    }
    else
    {
        Dts.Variables["User::DoWork"].Value = false;
    }
}

执行 SQL 查询将完整结果集存储为 OleDB 记录集。不能将其强制转换为字符串队列,即使 OleDB 记录集仅包含一列也是如此。因此,您必须在脚本任务中处理 OleDB 对象,填写您的队列。
以下代码示例片段可以处理 OleDB 对象变量

using System.Data.OleDb;
DataTable dt = new DataTable();
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.Fill(dt, Dts.Variables["User::CoreTables"].Value);
foreach (DataRow row in dt.Rows)
{
    //process datatable row here
}

最新更新