本文介绍了使用SSIS中的变量除了ScriptMain.cs另一类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SSIS一个C#脚本任务,我可以一个变量传递给没问题。我有我的脚本调用otherclass.cs创建另一个类。 ?如何使用otherclass.cs内的变量



我想只是这样做:

  _urlBase = Dts.Variables [用户:: URLBase] Value.ToString()。 



不过,我得到这个错误:

 DTS这个名字并不在目前的情况下
存在

我很新的C#和类,所以我可能无法正确​​问这个问题。就是我要求甚至可能吗?



感谢。






SOLUTION



下面是我的解决方案是什么,我试图完成的:



在ScriptMain.cs创建像这样的新服务:

  OtherService WS =新OtherService(Dts.Variables [用户:: URLBase] Value.ToString ()); 

在OtherService.cs我有这样的:

 类OtherService 
{
公共OtherService(字符串urlBase)
{
_urlBase = urlBase;
//做其他的东西
}
//其他方法和东西
}


解决方案

在2008年出价,请添加一个C#脚本任务,请注意,它会为您生成ScriptMain任务这一行:

 公共部分类ScriptMain:Microsoft.SqlServer.Dts.Tasks.ScriptTask.VS​​TARTScriptObjectModelBase 

这说ScriptMain是的 Microsoft.SqlServer.Dts.Tasks.ScriptTask.VS​​TARTScriptObjectModelBase 的子类。基类( VSTARTScriptObjectModelBase 的)定义类型的成员DTS的 Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel 的。这反过来有一个名为变量的成员。也就是说,当你在ScriptMain输入Dts.Variables你正在访问的。



您在otherclass.cs中定义的类不是VSTARTScriptObjectModelBase,因此子类不继承其成员DTS。这就是为什么编译器会告诉你DTS的名称在当前情况下不存在。



如果otherclass需要访问,然后一个变量的值作为cfrag建议你可当你实例化类的成员中,当你调用需要这个值类的成员函数将该值传递或。如果您需要访问多个变量,可以考虑这样的事情:

 公共otherclass(Microsoft.SqlServer.Dts.Runtime.Variables dtsVariables)
{
DtsVariables = dtsVariables;
//其他的东西
}
私人Microsoft.SqlServer.Dts.Runtime.Variables DtsVariables;

现在otherclass有一个实例成员名为DtsVariables其中的非静态成员可以使用它来访问的变量集合在创建对象时传入。你会实例并调用从ScriptMain方法是这样的:

  otherclass OC =新otherclass(Dts.Variables); 
字符串URL = oc.MakeURL();

在otherclass.MakeURL(),您可以使用实例变量DtsVariables就像你会使用DTS。在ScriptMain变量,例如:

 公共字符串MakeURL()
{
返回DtsVariables [用户: :URLBase] Value.ToString()+/default.aspx;
}


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

I tried to just do this:

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

But I get this error:

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

I'm pretty new to C# and classes so I may not be asking the question correctly. Is what I'm asking even possible?

Thanks.


SOLUTION

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

In ScriptMain.cs created new service like this:

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

In the OtherService.cs I had this:

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

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

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

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

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

If otherclass needs access to a variable's value then as cfrag suggests you can pass this value in when you instantiate a member of your class or when you call the class member function that needs this value. If you will 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;

Now otherclass has an instance member named DtsVariables which its non-static members can use to access the Variables collection passed in when the object was created. You would instantiate it and call methods from ScriptMain like this:

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

Inside otherclass.MakeURL(), you can use the instance variable DtsVariables like you would have used Dts.Variables in ScriptMain, e.g.:

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

这篇关于使用SSIS中的变量除了ScriptMain.cs另一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 14:33