SSIS 脚本任务和 SQL 批量插入的问题 - C#

Issues with SSIS Script Task and SQL Bulk Insert - C#

这是我正在尝试做的事情:

我正在使用执行 SQL 任务(在 ForEach 循环容器中)循环遍历大约 20 个 SQL 具有相同查询的服务器实例,每个实例显然都返回具有相同列的行.这将生成具有正确名称 (0) 和变量名称 User::TheResults.

的完整结果集

这个位似乎工作正常,即它没有错误。

然后我使用脚本任务 (C#) 将 SSIS 结果集填充到 DataTable 并将其批量复制到预先存在的 SQL 服务器 table.

这是 C# 代码...

    public void Main()
    {
        // TODO: Add your code here

        OleDbDataAdapter oleDA = new OleDbDataAdapter();
        DataTable dt = new DataTable();

        oleDA.Fill(dt, Dts.Variables["User::TheRecords"].Value.ToString());

        SqlConnection connection = new SqlConnection("Data Source=Server1\Instance1;Initial Catalog=iteration_test;Integrated Security=SSPI");

        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
        {
            bulkCopy.DestinationTableName = "dbo.versiontest";

            try
            {
                // Write from the source to the destination.
                bulkCopy.WriteToServer(dt);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        Dts.TaskResult = (int)ScriptResults.Success;
    }

执行任务时出现错误,但它不够明确,无法指出特定问题。它只是说明:

at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

我是一名DBA,目前从事大量BI工作,C#水平一般。我假设问题出在我的代码逻辑中。

所以回顾一下,我在 SSIS 中有一个结果集设置为变量,我想将其发送到 SQL 服务器 table。我在 SSIS 的脚本任务中使用上面的脚本来做到这一点。

出了什么问题?

从错误来看,脚本任务似乎无法识别您的变量。要解决此问题,请将变量 User::TheRecords 添加到脚本任务中的 ReadOnlyVariablesReadWriteVariables 集合。请参阅此 image 以供参考