0

I'm trying to make a dynamic connection to DB for all of my packages. I'm trying to achieve this by loading my user defined variables with a Script Component .

I know I can also use environment variables to do this, the problem is that not all of my packages will be scheduled and some of them I run with a web service or a procedure, so I can't use a scheduled job to do this, and using the @reference_id is a bit complicate to maintain.

I have no idea how can I loop the variables. I tried

foreach (Variable var in Variables)

But that's not the way ..
So I have two questions here:

  • How do I loop all the variables I passed?
  • How do I get the Variable name out of it ? (I don't see a name attribute on Variables.VarName.?)

E.G. The OLE DB source will return two rows:

conName | conString
 con1   |  someConnection
 con2   |  someConnection

And I have two user defined variables in my project -> con1 and con2 . So, iterate through my varaibles -> foreach(variable) , if var.name = row.conName , load with row.conString value .

5
  • Variables.YourVarialbName Commented Sep 25, 2017 at 11:41
  • @plaidDK It should be dynamic. The variable names can change, that's why I need to loop them all . Commented Sep 25, 2017 at 11:42
  • But you cannot loop an variable unless its an object Commented Sep 25, 2017 at 11:43
  • maybe this will help you: timmitchell.net/post/2015/04/20/… Commented Sep 25, 2017 at 11:45
  • @plaidDK Look at my answer, this is what I wanted. Commented Sep 25, 2017 at 14:48

1 Answer 1

1

Finally I've managed to do this. For those of you who will want to do the same, here is what I did :

public class ScriptMain : UserComponent
{

    Dictionary<string, string> AllCons = new Dictionary<string, string>();

    public override void PostExecute()
    {
        base.PostExecute();
        string connectionString;

        foreach (IDTSVariable100 obj in ReadWriteVariables)
        {
            if (AllCons.TryGetValue(obj.QualifiedName, out connectionString))
            {
                obj.Value = connectionString;
            }
        }
    }

    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        AllCons.Add("User::" + Row.ConName, Row.ConString);
    }
}

I used a Dictionary object to store the connection name and connection string on the Input0_ProcessInputRow section. Then, in the PostExecute, I've iterated through all of my User Defined Variables using an IDTSVariable100 Object. I then checked, if the connection name equals the variable, put the connection string in it.

Hope it helps someone.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad you got it to work. You could do the same as in the link i posted. Where you set your object to a datatable :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.