You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#应用中调用SSIS包按n个批次处理共享文件夹文件?

解决方案:C#控制台程序分批调用SSIS包处理共享文件夹文件

核心逻辑

先读取共享文件夹内的待处理文件,每次选取n个传入已有的SSIS包完成转换处理,成功后删除这批文件,循环执行直到所有文件处理完毕。

具体实现步骤

1. 项目依赖准备

在C#控制台项目中添加SSIS相关引用,可通过NuGet安装Microsoft.SqlServer.Management.IntegrationServices和Microsoft.SqlServer.Dts.Runtime包。

2. 分批处理代码实现

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Microsoft.SqlServer.Dts.Runtime;

namespace SSISBatchFileProcessor
{
    class Program
    {
        static void Main(string[] args)
        {
            // 自定义配置参数
            string sharedFolder = @"\\your-shared-folder-path";
            int batchCount = 5; // 每次处理的文件数量n
            string ssisPackagePath = @"C:\your-package-path\FileProcess.dtsx";
            string sqlInstance = "localhost"; // 若使用本地.dtsx包可忽略此参数

            while (true)
            {
                // 按创建时间取最新的n个待处理文件
                var targetFiles = new DirectoryInfo(sharedFolder)
                    .GetFiles()
                    .OrderBy(f => f.CreationTime)
                    .Take(batchCount)
                    .ToList();

                if (targetFiles.Count == 0) break;

                // 将文件路径拼接为分号分隔的字符串,传递给SSIS包
                string filePaths = string.Join(";", targetFiles.Select(f => f.FullName));

                // 调用SSIS包执行处理
                bool processSuccess = RunSSISPackage(ssisPackagePath, sqlInstance, filePaths);

                if (processSuccess)
                {
                    // 删除已处理完成的文件
                    foreach (var file in targetFiles)
                    {
                        try
                        {
                            File.Delete(file.FullName);
                            Console.WriteLine($"已删除:{file.Name}");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"删除失败 {file.Name}:{ex.Message}");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("当前批次处理失败,终止循环");
                    break;
                }

                Console.WriteLine($"当前批次完成,剩余文件数:{new DirectoryInfo(sharedFolder).GetFiles().Length}");
            }

            Console.WriteLine("所有文件处理完成");
        }

        /// <summary>
        /// 执行SSIS包并返回执行结果
        /// </summary>
        static bool RunSSISPackage(string packagePath, string serverInstance, string fileList)
        {
            try
            {
                Application ssisApp = new Application();
                Package package = ssisApp.LoadPackage(packagePath, null);

                // 给SSIS包的自定义变量赋值(需提前在SSIS包中创建User::FileList变量)
                package.Variables["User::FileList"].Value = fileList;

                // 执行包并返回结果
                DTSExecResult execResult = package.Execute();
                return execResult == DTSExecResult.Success;
            }
            catch (Exception ex)
            {
                Console.WriteLine("SSIS包执行出错:" + ex.Message);
                return false;
            }
        }
    }
}

3. SSIS包适配调整

  • 在你的SSIS包中添加字符串类型的用户变量User::FileList
  • 在包的处理逻辑里,拆分FileList变量获取本次要处理的所有文件路径,逐个执行转换操作
  • 确保SSIS包执行完成后返回正确的成功/失败状态

4. 关键注意事项

  • 确保控制台程序拥有共享文件夹的读写权限,以及SSIS包执行所需的系统权限
  • 若SSIS包部署在SQL Server SSIS目录中,可改用IntegrationServices对象调用,替换本地包加载逻辑
  • 可添加日志记录模块,方便后续排查问题
  • 处理文件时建议添加文件锁,避免其他程序同时操作同一文件

内容的提问来源于stack exchange,提问作者Sreepathi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 17:36:31