我正在尝试使用C#(。Net 4.0)和ManagedDTS(10.0)生成一些SSIS(2008)包。到目前为止,我已经设法生成一个包并添加一些连接和变量,现在我正在尝试填充控制流,我尝试添加的第一个任务是“执行SQL任务”来检查源表是否有任何行,所以我可以中止,如果它没有。如果我手动构建SSIS包,我将如何执行此操作是让执行SQL任务运行如下查询:
SELECT RecordExists = CASE WHEN EXISTS (SELECT * FROM [Schema].[TABLE] WHERE [COLUMN] IS NULL) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END
然后我将ResultSet属性设置为“Single row”,然后将Result映射到变量:
但是我无法在C#中看到如何做到这一点,这是我到目前为止所做的:
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask;
...
public Package SsisPackage;
...
SsisPackage.Variables.Add("VAR_RecordExists", false, "User", false);
Executable RecordExists = SsisPackage.Executables.Add("STOCK:SQLTask");
TaskHost thRecordExists = RecordExists as TaskHost;
thRecordExists.Properties["Name"].SetValue(thRecordExists, "Do Records Exist?");
thRecordExists.Properties["SqlStatementSource"].SetValue(thRecordExists, "SELECT RecordExists = CASE WHEN EXISTS (SELECT * FROM [Schema].[TABLE] WHERE [COLUMN] IS NULL) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END");
thRecordExists.Properties["Connection"].SetValue(thRecordExists, "Stage");
thRecordExists.Properties["ResultSetType"].SetValue(thRecordExists, ResultSetType.ResultSetType_SingleRow);
我的第一个问题是最后一行错误与错误: “ResultSetType”名称在当前上下文中不存在
我的第二个问题是,即使我开始工作,我也不知道如何将结果集实际映射到变量,我一直在使用这里的文档,但它没有提到映射结果集的任何内容:{ {3}}。然后我找到了这个:https://docs.microsoft.com/en-us/sql/integration-services/building-packages-programmatically/adding-tasks-programmatically告诉我如何设置结果集类型但导致上述错误。它仍然没有提到如何设置结果集映射。
感谢任何帮助。
答案 0 :(得分:0)
所以我没有找到使用ManagedDTS框架的方法,但我确实让它工作。
我最终做的是:
我为此创建了一个帮助器类,虽然它可能不是最有效或最干净的代码(我通常不是一个C#开发人员)但它确实可以完成这项任务:
public class ResultSetBinding
{
string ResultName;
string DtsVariableName;
string TaskName;
public ResultSetBinding(string taskName, string resultName, string dtsVariableName)
{
TaskName = taskName;
ResultName = resultName;
DtsVariableName = dtsVariableName;
}
public void AddResultBinding(string filePath)
{
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlElement root = doc.DocumentElement;
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("DTS", "www.microsoft.com/SqlServer/Dts");
nsmgr.AddNamespace("SQLTask", "www.microsoft.com/sqlserver/dts/tasks/sqltask");
XmlNodeList executableNodes = root.SelectNodes("//DTS:Executable", nsmgr);
foreach (XmlNode executableNode in executableNodes)
{
if (IsExecuteSQLTask(executableNode, nsmgr))
{
if (IsSpecifiedTask(executableNode, nsmgr))
{
AddResultBindingToNode(doc, executableNode, nsmgr);
}
}
}
doc.PreserveWhitespace = true;
doc.Save(filePath);
}
private bool IsExecuteSQLTask(XmlNode executableNode, XmlNamespaceManager nsmgr)
{
foreach (XmlAttribute executableAttribute in executableNode.Attributes)
{
if (executableAttribute.Name == "DTS:ExecutableType")
{
return executableAttribute.Value.Contains("ExecuteSQLTask");
}
}
return false;
}
private bool IsSpecifiedTask(XmlNode executableNode, XmlNamespaceManager nsmgr)
{
foreach (XmlNode propertyNode in executableNode.ChildNodes)
{
if (propertyNode.Name == "DTS:Property")
{
foreach (XmlAttribute propertyAttribute in propertyNode.Attributes)
{
if (propertyAttribute.Name == "DTS:Name" && propertyAttribute.Value == "ObjectName" && propertyNode.InnerText == TaskName)
{
return true;
}
}
}
}
return false;
}
private void AddResultBindingToNode(XmlDocument doc, XmlNode executableNode, XmlNamespaceManager nsmgr)
{
foreach (XmlNode objectNode in executableNode.ChildNodes)
{
if (objectNode.Name == "DTS:ObjectData")
{
foreach (XmlNode sqlNode in objectNode.ChildNodes)
{
if (sqlNode.Name == "SQLTask:SqlTaskData")
{
XmlElement bindingElement = doc.CreateElement("SQLTask:ResultBinding", "www.microsoft.com/sqlserver/dts/tasks/sqltask");
bindingElement.SetAttribute("ResultName", "www.microsoft.com/sqlserver/dts/tasks/sqltask", ResultName);
bindingElement.SetAttribute("DtsVariableName", "www.microsoft.com/sqlserver/dts/tasks/sqltask", String.Format("User::{0}", DtsVariableName));
sqlNode.AppendChild(bindingElement);
}
}
}
}
}
}
然后我设置了以下类成员:
public List<ResultSetBinding> ResultBindings;
创建任务时添加结果绑定:
ResultSetBinding ResultBinding = new ResultSetBinding("Do Records Exist?", "RecordExists", "VAR_RecordExists");
ResultBindings.Add(ResultBinding);
然后一旦保存循环并添加结果绑定:
foreach (ResultSetBinding ResultBinding in ResultBindings)
{
ResultBinding.AddResultBinding(FilePath);
}