c# - Set Script Task code dynamically in SSIS 2012 -
in application, script task created dynamically.
in sql server 2008's implementation of ssis, following method worked fine.
private void setsourcecode(scripttask scripttask, string code, string codename) { string filename = "scriptmain.vb"; string language = "visualbasic"; string proj = ".vbproj"; scripttask.scriptlanguage = vstascriptlanguages.getdisplayname(language); scripttask.scriptingengine.initnewscript(language, scripttask.scriptprojectname, proj); scripttask.scriptingengine.showdesigner(false); scripttask.scriptingengine.addcodefile(filename, code); if (!scripttask.scriptingengine.build()) throw new exception("failed build vb script code: " + codename); scripttask.scriptingengine.savescripttostorage(); if (!scripttask.scriptingengine.closeide(false)) { throw new exception("unable close scripting engine."); } }
how migrate code sql server 2012, because following methods removed sql server 2012 dll-s (assemblies):
- initnewscript
- addprojectreference
- addcodefile
- savescripttostorage
- closeide
- build
- showdesigner
generally, how dynamically set source code script task in sql server 2012?
as you've noticed, vsta helper methods use in 2008 moved/removed in 2012. still possible do, code has changed.
the easiest thing load existing project using vstahelper.loadprojectfromfolder().
if want dynamically add script files, see snippet below. there 2 main things need keep in mind:
the scriptingengine , vstahelper classes represent vsta itself. you’d create project, , add new files. cannot remove or replace existing file directly here. when call saveprojectostorage(), it's closing vsta window … saves project , compiled binary scripttask.
scripttask.scriptstorage allows directly manipulate source file contents. here, can modify content of file.
the following code snippet should started.
static void main(string[] args) { // 1. create new package, , add script task var pkg = new package(); var exec = pkg.executables.add("stock:scripttask"); var th = (taskhost)exec; th.name = "script task"; th.description = "this script task"; var task = (scripttask)th.innerobject; // 2. set script language - "csharp" or "visualbasic" task.scriptlanguage = vstascriptlanguages.getdisplayname("csharp"); // 3. set variables used script //task.readwritevariables = "user::var1, user::var2"; // 4. create new project template located in default path task.scriptingengine.vstahelper.loadnewproject(task.projecttemplatepath, null, "myscriptproject"); // 5. initialize designer project, add new code file, , build //task.scriptingengine.vstahelper.initalize("", true); //task.scriptingengine.vstahelper.addfiletoproject("xx.cs", "filecontents"); //task.scriptingengine.vstahelper.build(""); // 6. persist vsta project + binary task if (!task.scriptingengine.saveprojecttostorage()) { throw new exception("save failed"); } // 7. use following code replace scriptmain contents var contents = file.readalltext("path file"); var scriptfile = task.scriptstorage.scriptfiles["scriptmain.cs"] = new vstascriptprojectstorage.vstascriptfile(vstascriptprojectstorage.encoding.utf8, contents); // 8. reload script project, build , save task.scriptingengine.loadprojectfromstorage(); task.scriptingengine.vstahelper.build(""); // 9. persist vsta project + binary task if (!task.scriptingengine.saveprojecttostorage()) { throw new exception("save failed"); } // 10. cleanup task.scriptingengine.disposevstahelper(); // 11. save string xml; pkg.savetoxml(out xml, null); file.writealltext(@"c:\temp\package.dtsx", xml); }
Comments
Post a Comment