Create a propery of type string. Store the name of a variable in that property. Use the property value to locate the variable in the variables collection at run-time, lock and read the variable's value.
What are you asking? How to create a property? How to store a string in a property, the string being a variable name? How to read a string value from a property and get the matching varible and it's value at runtime?
You should also validate the value of the property during Validate of course, and check tht it is a valid variable name.
|||
DarrenSQLIS wrote:
Create a propery of type string. Store the name of a variable in that property. Use the property value to locate the variable in the variables collection at run-time, lock and read the variable's value.
What are you asking? How to create a property? How to store a string in a property, the string being a variable name? How to read a string value from a property and get the matching varible and it's value at runtime?
You should also validate the value of the property during Validate of course, and check tht it is a valid variable name.
So provide a custom IDTSCustomProperty90 in ProvideComponentProperties() and let users type in the variable name is enough(of course, I should also follow what you said)?
I wanted to make it behave the same way as the FileNameVariable property of Raw File Destination, that is: users don't type in the variable name, instead, they select from all available variable names.
|||
DarrenSQLIS wrote:
Create a propery of type string. Store the name of a variable in that property. Use the property value to locate the variable in the variables collection at run-time, lock and read the variable's value.
What are you asking? How to create a property? How to store a string in a property, the string being a variable name? How to read a string value from a property and get the matching varible and it's value at runtime?
You should also validate the value of the property during Validate of course, and check tht it is a valid variable name.
How can I get the matching variable and it's value at runtime? And generally in which method should I do that? Thanks.
|||
Here's a sample from a component I developed to raise custom events from a package's control flow; the same basic technique should work for you in data flow as well.
First, create a property that stores the variable name. This will be set by the package developer at design time.
Code Snippet
private string variableName;
///
/// This property gets or sets the name of the variable in which the
/// event text is stored.
///
[Browsable(true)]
[Category("Custom")]
[Description("Gets or sets the name of the variable in which the event text is stored.")]
public string VariableName
{
get
{
return variableName;
}
set
{
if (value.IndexOf("::") == -1)
{
variableName = "User::" + value;
}
else
{
variableName = value;
}
}
}
(The set accessor for the property is simply explicitly adding the User namespace to the specified variable name unless a namespace is already specified. I've yet to add a drop-down so the package developer can pick from a list of available variables.)
Then, you can use the component's VariableDispenser object (which is passed in a parameter to many of the methods you overload when developing your component) to lock and work with the package variable identified by the property. Here's a sample from the Validate method in my component:
Code Snippet
Variables vars = null;
DTSExecResult result;
variableDispenser.LockOneForRead(variableName, ref vars);
if (vars[variableName].DataType != TypeCode.String)
{
// The variable is accessable, but is not the correct data type
componentEvents.FireError(0, SubComponent(variableDispenser),
string.Format("Variable '{0}' is not a String variable", variableName), "", 0);
result = DTSExecResult.Failure;
}
else
{
result = DTSExecResult.Success;
}
vars.Unlock();
return result;
The key thing here is that you're using the variableDispenser to lock the specific variable identified by the component's property, working with it, and then unlocking it when you're done.
(The SubComponent method which is referenced in the code sample above is just a little helper routine that returns a string in a standard format to identify where an error or event is coming from. I need this type of string in many places, so I have a function that can be reused, but it's not really relevant to your question...)
Hope this helps!
No comments:
Post a Comment