属性网格下拉列表中的C#SSIS自定义控制流任务列表变量
我第一次在C#中编写了一个自定义控件流SSIS任务。在我的任务UI编辑器上,我有一个属性网格,在其中一个选项中,我希望能够填充任何可用任务变量的下拉列表,并为用户提供创建新任务变量的选项。我已经研究了几天,在论坛上找到了一些很好的例子,但现在我有点迷路了。我的代码如下编译,编辑器显示一个下拉列表,但其为空。通过它之后,它似乎下降到这一行:属性网格下拉列表中的C#SSIS自定义控制流任务列表变量,c#,winforms,ssis,propertygrid,C#,Winforms,Ssis,Propertygrid,我第一次在C#中编写了一个自定义控件流SSIS任务。在我的任务UI编辑器上,我有一个属性网格,在其中一个选项中,我希望能够填充任何可用任务变量的下拉列表,并为用户提供创建新任务变量的选项。我已经研究了几天,在论坛上找到了一些很好的例子,但现在我有点迷路了。我的代码如下编译,编辑器显示一个下拉列表,但其为空。通过它之后,它似乎下降到这一行: taskHostProperty = context.Instance.GetType().GetProperty("TransferTask", typeo
taskHostProperty = context.Instance.GetType().GetProperty("TransferTask", typeof(TaskHost));The "TransferTask" being the name of my control flow task. I'm wondering if this is correct?
我的完整代码如下
//Property Grid Property
[Category("General"),
Description("Specifies the local Path for this task"),
Browsable(true),
ReadOnly(false),
DesignOnly(false),
TypeConverter(typeof(VariableConverter)),
DisplayName("Local Path")]
public string LocalPath
{
get
{
return this.stLocalPath;
}
set
{
dtsVariableService = serviceProvider.GetService(typeof(IDtsVariableService)) as IDtsVariableService;
dtsVariableService.PromptAndCreateVariable(parentWindow, dtsContainer,"Local Path","User",typeof(string));
this.stLocalPath = value;
}
}
//Variable Converter
internal class VariableConverter : TypeConverter
{
StandardValuesCollection svc = new StandardValuesCollection(new ArrayList());
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
TaskHost taskHost = null;
PropertyInfo taskHostProperty = null;
List<string> values = new List<string>();
values.Add(NEW_VARIABLE);
if (context == null)
{
return svc;
}
if (context.Instance == null)
{
return svc;
}
taskHostProperty = context.Instance.GetType().GetProperty("TransferTask", typeof(TaskHost));
if (taskHostProperty == null)
{
return svc;
}
taskHost = taskHostProperty.GetValue(context.Instance, null) as TaskHost;
foreach(Variable v in taskHost.Variables)
{
if (!v.SystemVariable && v.DataType == TypeCode.String)
{
values.Add(v.QualifiedName);
}
}
values.Sort();
return new StandardValuesCollection(values);
}
//属性网格属性
[类别(“一般”),
说明(“指定此任务的本地路径”),
可浏览(真),
只读(错误),
仅设计(错误),
TypeConverter(typeof(VariableConverter)),
DisplayName(“本地路径”)]
公共字符串本地路径
{
得到
{
返回此.stLocalPath;
}
设置
{
dtsVariableService=serviceProvider.GetService(typeof(IDtsVariableService))作为IDtsVariableService;
PromptAndCreateVariable(父窗口,dtsContainer,“本地路径”,“用户”,类型(字符串));
this.stLocalPath=值;
}
}
//可变转换器
内部类VariableConverter:TypeConverter
{
StandardValuesCollection svc=新的StandardValuesCollection(新的ArrayList());
公共覆盖布尔GetStandardValuesSupported(ITypeDescriptorContext上下文)
{
返回true;
}
公共覆盖标准值集合GetStandardValues(ITypeDescriptor上下文)
{
TaskHost TaskHost=null;
PropertyInfo taskHostProperty=null;
列表值=新列表();
添加(新的_变量);
if(上下文==null)
{
返回svc;
}
if(context.Instance==null)
{
返回svc;
}
taskHostProperty=context.Instance.GetType().GetProperty(“TransferTask”,typeof(TaskHost));
如果(taskHostProperty==null)
{
返回svc;
}
taskHost=taskHostProperty.GetValue(context.Instance,null)作为taskHost;
foreach(taskHost.Variables中的变量v)
{
如果(!v.SystemVariable&&v.DataType==TypeCode.String)
{
添加(v.QualifiedName);
}
}
value.Sort();
返回新的标准值集合(值);
}
最后,我放弃了将变量添加到属性网格的尝试,只是创建了自己的表单。我使用以下代码填充组合框,并在允许用户添加新的SSIS变量之后。我刷新了数据源。我仍然想知道如何正确地执行此操作,但我没有时间坐下来解决它。
我使用以下方法首先获得了所需的SSIS变量,我可以添加一个新的SSIS变量,并且可以选择新创建的SSIS变量。我相信有更好的方法可以做到这一点,但目前还可以
public ObservableCollection<string> FillVariableList()
{
ObservableCollection<string> variables = new ObservableCollection<string>();
variables.Add(string.Empty);
variables.Add(New_Variable);
foreach (Variable v in thetaskHost.Variables)
{
if (!v.SystemVariable && v.DataType == TypeCode.String && !variables.Contains(v.Name))
{
variables.Add(v.Name);
}
}
return variables;
}
private void cmbxVariables_SelectionChangeCommitted(object sender, EventArgs e)
{
if (cmbxVariables.Text == New_Variable)
{
try
{
DtsContainer dtsContainer = null;
dtsVariableServie = serviceProvider.GetService(typeof(IDtsVariableService)) as IDtsVariableService;
Variable var = dtsVariableServie.PromptAndCreateVariable(null, dtsContainer, "VariableName", "User", typeof(string));
if (!var.IsNull())
{
cmbxVariables.DataSource = null;
cmbxVariables.DataSource = FillVariableList();
}
}
catch (Exception exe)
{
MessageBox.Show(exe.ToString());
}
}
else
{
foreach (Variable v in thetaskHost.Variables)
{
if (v.Name == cmbxVariables.Text)
{
//Do something with the variable selected
break;
}
}
}
}