Use SSIS variable in other class besides ScriptMain.cs

I have a C # script task in SSIS that I can pass a variable with no problem. I have another class that I created in a script call to otherclass.cs. How do I use a variable in otherclass.cs?

I tried to just do this:

_urlBase = Dts.Variables["User::URLBase"].Value.ToString();

      

But I am getting this error:

The name 'Dts' does not exist in the current context

      

I'm new to C # and classes, so I can't seem to ask the question correctly. Is what I'm asking is even possible?

Thank.


DECISION

Here is my solution to what I was trying to accomplish:

A new service is created in ScriptMain.cs, for example:

OtherService ws = new OtherService(Dts.Variables["User::URLBase"].Value.ToString());

      

In OtherService.cs I had this:

class OtherService
{
    public OtherService(string urlBase)
    {
        _urlBase = urlBase;
        //do other stuff
    }
    //other methods and stuff
}

      

+3


source to share


1 answer


When you add a C # Script task to BIDS 2008, notice this line in the ScriptMain task it creates for you:

public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

      

This suggests that ScriptMain is a subclass of Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase. This base class (VSTARTScriptObjectModelBase) defines a Dts member of type Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel. This, in turn, has a member named Variables. This is what you are referring to when you enter "Dts.Variables" in ScriptMain.

The class you define in "otherclass.cs" is not a subclass of VSTARTScriptObjectModelBase and therefore does not inherit from its Dts member. This is why the compiler tells you "The name" Dts "does not exist in the current context."

If another class needs access to the value of a variable, then, as cfrag suggests, you can pass that value when instantiating a member of your class, or when calling a class member function that needs that value. If you need to access multiple variables, consider something like this:



public otherclass(Microsoft.SqlServer.Dts.Runtime.Variables dtsVariables)
{
    DtsVariables = dtsVariables;
    // other stuff
}
private Microsoft.SqlServer.Dts.Runtime.Variables DtsVariables;

      

The otherclass now has an instance member named DtsVariables that its non-static members can use to access the Variables collection passed when the object was created. You have to instantiate and call methods from ScriptMain like this:

otherclass oc = new otherclass(Dts.Variables);
string url = oc.MakeURL();

      

Inside otherclass.MakeURL () you can use an instance variable of DtsVariables, for example you would use Dts.Variables in ScriptMain, for example:

public string MakeURL()
{
    return DtsVariables["User::URLBase"].Value.ToString() + "/default.aspx";
}

      

+7


source







All Articles