如何从事件处理程序发送 Dts 信息消息 SSIS?
How to send Dts information messages SSIS from event handler?
我有一个带有 Script Task
的 SSIS 程序包,它调用一些基本的 C#
代码。我正在使用 SQL SMO 将表列表从 DB1 复制到 DB2。
我有那个工作,但我想做的是订阅 SMO 事件处理程序并将进度消息发送回 SSIS,这样当 SSIS 包是 运行 时,我会知道什么正在做。
我找到了要使用的事件处理程序,但问题是我无法访问 Dts
变量来发送消息...我做错了什么?
public static void DataTransferReport(object sender, DataTransferEventArgs args)
{
bool fireAgain = false;
// This line does not work. sender is `Transfer`
var Dts = sender as Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel;
Dts.Events.FireInformation(2, "MySubComponent", "My Message Test", "", 0, ref fireAgain);
}
protected static void DiscoveryProgressReport(object sender, ProgressReportEventArgs args)
{
Console.WriteLine("1[" + args.Current.Value + "]");
}
protected static void ScriptingProgressReport(object sender, ProgressReportEventArgs args)
{
Console.WriteLine("2[" + args.Current.Value + "]");
}
protected static void ScriptingErrorReport(object sender, ScriptingErrorEventArgs args)
{
Console.WriteLine("3[" + args.Current.Value + "]");
}
下面的缩写 main 方法显示了我如何调用事件处理程序。
public void Main()
{
// Some code above...
// Setup the transfer and parameters
Transfer transfer = new Transfer
{
Database = sourceDatabase,
//set destination server and database
DestinationServer = edwConnection.Name,
DestinationDatabase = edwConnection.Databases[targetDatabaseName].Name
};
//include data
transfer.CopyData = true;
bool fireAgain = false;
// Loop over each table candidate var
foreach (string tableName in tableNames)
{
// Create a table object
Table table = sourceDatabase.Tables[tableName];
// verify it exists and is not a system table
if (table != null && !table.IsSystemObject)
{
// Add table object to transfer list
transfer.ObjectList.Add(table);
}
}
// Subscribe event handlers
transfer.DataTransferEvent += new DataTransferEventHandler(DataTransferReport);
transfer.DiscoveryProgress += new ProgressReportEventHandler(DiscoveryProgressReport);
transfer.ScriptingProgress += new ProgressReportEventHandler(ScriptingProgressReport);
transfer.ScriptingError += new ScriptingErrorEventHandler(ScriptingErrorReport);
// DO THE WORK!
transfer.TransferData();
// More code below...
}
我可能没有理解正确...但是你不能只删除 static
就可以吗?这是您的方法之一,您需要对其他方法执行相同的操作。 Sender
仍将是您订阅事件的 Transfer
对象。
public void DataTransferReport(object sender, DataTransferEventArgs args)
{
bool fireAgain = false;
Dts.Events.FireInformation(2, "MySubComponent", "My Message Test", "", 0, ref fireAgain);
}
我有一个带有 Script Task
的 SSIS 程序包,它调用一些基本的 C#
代码。我正在使用 SQL SMO 将表列表从 DB1 复制到 DB2。
我有那个工作,但我想做的是订阅 SMO 事件处理程序并将进度消息发送回 SSIS,这样当 SSIS 包是 运行 时,我会知道什么正在做。
我找到了要使用的事件处理程序,但问题是我无法访问 Dts
变量来发送消息...我做错了什么?
public static void DataTransferReport(object sender, DataTransferEventArgs args)
{
bool fireAgain = false;
// This line does not work. sender is `Transfer`
var Dts = sender as Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel;
Dts.Events.FireInformation(2, "MySubComponent", "My Message Test", "", 0, ref fireAgain);
}
protected static void DiscoveryProgressReport(object sender, ProgressReportEventArgs args)
{
Console.WriteLine("1[" + args.Current.Value + "]");
}
protected static void ScriptingProgressReport(object sender, ProgressReportEventArgs args)
{
Console.WriteLine("2[" + args.Current.Value + "]");
}
protected static void ScriptingErrorReport(object sender, ScriptingErrorEventArgs args)
{
Console.WriteLine("3[" + args.Current.Value + "]");
}
下面的缩写 main 方法显示了我如何调用事件处理程序。
public void Main()
{
// Some code above...
// Setup the transfer and parameters
Transfer transfer = new Transfer
{
Database = sourceDatabase,
//set destination server and database
DestinationServer = edwConnection.Name,
DestinationDatabase = edwConnection.Databases[targetDatabaseName].Name
};
//include data
transfer.CopyData = true;
bool fireAgain = false;
// Loop over each table candidate var
foreach (string tableName in tableNames)
{
// Create a table object
Table table = sourceDatabase.Tables[tableName];
// verify it exists and is not a system table
if (table != null && !table.IsSystemObject)
{
// Add table object to transfer list
transfer.ObjectList.Add(table);
}
}
// Subscribe event handlers
transfer.DataTransferEvent += new DataTransferEventHandler(DataTransferReport);
transfer.DiscoveryProgress += new ProgressReportEventHandler(DiscoveryProgressReport);
transfer.ScriptingProgress += new ProgressReportEventHandler(ScriptingProgressReport);
transfer.ScriptingError += new ScriptingErrorEventHandler(ScriptingErrorReport);
// DO THE WORK!
transfer.TransferData();
// More code below...
}
我可能没有理解正确...但是你不能只删除 static
就可以吗?这是您的方法之一,您需要对其他方法执行相同的操作。 Sender
仍将是您订阅事件的 Transfer
对象。
public void DataTransferReport(object sender, DataTransferEventArgs args)
{
bool fireAgain = false;
Dts.Events.FireInformation(2, "MySubComponent", "My Message Test", "", 0, ref fireAgain);
}