SSIS 小脚本 - 文件路径验证
2013-01-14 11:00 BIWORK 阅读(1677) 评论(0) 编辑 收藏 举报之前项目中经常有文件的读取或者输出操作,其中最重要的就是在处理文件输入/输出之前验证文件的路径是否存在,如果不存在就输出错误.
/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
namespace ST_TEST.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
string sFilePath;
string sPackagename;
// Get the package name from SSIS Variables
sPackagename = Dts.Variables["System::PackageName"].Value.ToString();
// Get the file path from SSIS Variables
sFilePath = Dts.Variables["User::IncomingFile"].Value.ToString();
try
{
// Check for existence of file
if ( !File.Exists( sFilePath ) )
{
Dts.Events.FireError( 0,
sPackagename,
"File at file path: " + sFilePath + " does not exist",
"",
0 );
}
}
catch ( System.Exception e )
{
Dts.Events.FireError(0,
sPackagename,
"Exception occurred check for file at file path: " + sFilePath + " with error: " + e.Message.ToString(),
"",
0);
}
}
}
}
上面的 User::IncomingFile 在传入Script Component之前通过变量表达式就已经将文件夹路径, 文件名路径拼写在一起形成一个完整的文件路径, 所以进来后直接去验证和处理.
有时如果在输出文件之时,文件的输入目录和文件夹地址并不是固定的,而是通过变量来维护的. 在文件输出之前需要检查下文件输出的的目录和文件夹是否存在, 是否能够构成一个有效的输出路径,因此需要这样来检查下.
string directory = Dts.Variables["User::OutgoingDirectory"].Value.ToString();
string folder = Dts.Variables["User::OutgoingFolder"].Value.ToString();
string folderPath = Path.Combine(directory,folder);
if (!Directory.Exists(folderPath))
{
Dts.Events.FireError(0,sPackageName,"Cannot find this folder path "+folderPath+" ","",0);
return;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库