要想在脚本组件中访问包变量,首先必须设置脚本组件2个属性的值,如下
readonlyvariables
readwritevariables
这2值指定了哪些变量可以访问,哪些变量可以改写(如有多个变量则用逗号分隔),如果你没有指定上面2个属性的值,则不能在脚本组件的代码中访问包变量
下面我举一个从文件中加载内容到包变量的一个例子
 1、首先我们定义2个变量 filename 和 filecontents ,并指定其类型为 string
 2、拖曳一个脚本组件到控制面板上,并设置 readonlyvariables和readwritevariables 属性的值分别为 filename 、filecontents
 3、设计脚本组件的代码,如下 
 
 public sub main()
         dim errorinfo as string = ""
         dim contents as string = ""
 
         contents = getfilecontents(dts.variables("filename").value, errorinfo)
         if errorinfo.length > 0 then
             msgbox(errorinfo, msgboxstyle.critical, "error")
             dts.taskresult = dts.results.failure
         else
             msgbox(contents, msgboxstyle.okonly, "file contents")
             dts.variables("filecontents").value=contents
             dts.taskresult = dts.results.success
         end if
  end sub
 public function getfilecontents(byval filepath as string, optional byval errorinfo as string = "") as string
        dim strcontents as string
        dim objreader as streamreader
        try
            objreader = new streamreader(filepath)
            strcontents = objreader.readtoend()
            objreader.close()
            return strcontents
        catch ex as exception
            errorinfo = ex.message
        end try
    end function
新闻热点
疑难解答