Cro*_*ros 3 sql sql-server ssis sql-server-2008
我正在编写一个自定义SSIS任务,作为其中一个函数,应该在数据库连接上执行存储过程.我似乎无法找到有关如何完成此操作的任何信息.
我正在使用ADO.NET连接管理器连接到数据库,我希望用C#编写我的任务.
在自定义SSIS任务中执行SQL的首选方法是什么?
答案取决于您使用什么连接管理器连接到数据库,但一般方法是相同的:
Connections
属性在自定义任务中获取相关的连接管理器Package
.AcquireConnection
在连接管理器上调用该方法以获取与数据库的连接.此方法允许您利用SSIS提供的连接的配置和管理.
对于ADO.NET连接管理器,可以使用以下代码:
public override DTSExecResult Validate(
Connections connections, VariableDispenser variableDispenser,
IDTSComponentEvents componentEvents, IDTSLogging log)
{
// Validate connection exists.
if(!connections.Contains("YourConnection"))
{
componentEvents.FireError(0, "CustomTask",
"Invalid connection manager.", "", 0);
return DTSExecResult.Failure;
}
return DTSExecResult.Success;
}
public override DTSExecResult Execute(Connections connections,
VariableDispenser variableDispenser, IDTSComponentEvents componentEvents,
IDTSLogging log, object transaction)
{
ConnectionManager cm = connections["YourConnection"];
try
{
SqlConnection connection
= cm.AcqureConnection(transaction) as SqlConnection;
if(connection == null)
{
componentEvents.FireError(0, "CustomTask",
"Failed to acquire ADO.NET connection.", "", 0);
Return DTSExecResult.Failure;
}
// TODO: Use connection to execute SQL.
}
catch(Exception ex)
{
componentEvents.FireError(0, "CustomTask",
ex.Message, "", 0);
Return DTSExecResult.Failure;
}
}
Run Code Online (Sandbox Code Playgroud)
您将需要一些更好的错误处理,我不知道如何处理连接的生命周期,无论您是应该手动打开还是在使用后处置.
祝好运!
归档时间: |
|
查看次数: |
774 次 |
最近记录: |