sql >> Base de Datos >  >> RDS >> Sqlserver

Establecer código de tarea de secuencia de comandos de forma dinámica en SSIS 2012

Como habrás notado, Métodos auxiliares de VSTA que podía usar en 2008 se movieron/eliminaron en 2012. Todavía es posible hacerlo, pero el código ha cambiado.

Lo más fácil es cargar un proyecto existente usando VstaHelper.LoadProjectFromFolder ().

Si desea agregar dinámicamente archivos de secuencias de comandos, consulte el fragmento a continuación. Hay dos cosas principales que debe tener en cuenta:

Las clases ScriptingEngine y VstaHelper representan el propio VSTA. Aquí es donde crearía el proyecto y agregaría nuevos archivos. No puede eliminar o reemplazar un archivo existente directamente aquí. Cuando llama a SaveProjecToStorage(), es como cerrar la ventana de VSTA... guarda el proyecto y el binario compilado en ScriptTask.

ScriptTask.ScriptStorage le permite manipular directamente el contenido del archivo fuente. Desde aquí, puede modificar el contenido de un archivo.

El siguiente fragmento de código debería ayudarlo a comenzar.

static void Main(string[] args)
{
    // 1. Create new package, and add a script task
    var pkg = new Package();
    var exec = pkg.Executables.Add("STOCK:ScriptTask");
    var th = (TaskHost)exec;
    th.Name = "Script Task";
    th.Description = "This is a Script Task";
    var task = (ScriptTask)th.InnerObject;

    // 2. Set the script language - "CSharp" or "VisualBasic"
    task.ScriptLanguage = VSTAScriptLanguages.GetDisplayName("CSharp");

    // 3. Set any variables used by the script
    //task.ReadWriteVariables = "User::Var1, User::Var2";

    // 4. Create a new project from the template located in the default path
    task.ScriptingEngine.VstaHelper.LoadNewProject(task.ProjectTemplatePath, null, "MyScriptProject");

    // 5. Initialize the designer project, add a new code file, and build
    //task.ScriptingEngine.VstaHelper.Initalize("", true);
    //task.ScriptingEngine.VstaHelper.AddFileToProject("XX.cs", "FileContents");
    //task.ScriptingEngine.VstaHelper.Build("");

    // 6. Persist the VSTA project + binary to the task
    if (!task.ScriptingEngine.SaveProjectToStorage())
    {
        throw new Exception("Save failed");
    }

    // 7. Use the following code to replace the ScriptMain contents
    var contents = File.ReadAllText("path to file");
    var scriptFile =
        task.ScriptStorage.ScriptFiles["ScriptMain.cs"] =
        new VSTAScriptProjectStorage.VSTAScriptFile(VSTAScriptProjectStorage.Encoding.UTF8, contents);


    // 8. Reload the script project, build and save
    task.ScriptingEngine.LoadProjectFromStorage();
    task.ScriptingEngine.VstaHelper.Build("");

    // 9. Persist the VSTA project + binary to the 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);
}