要想在腳本組件中訪問包變量,首先必須設置腳本組件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
|
新聞熱點
疑難解答