Created
November 28, 2013 17:05
-
-
Save billinkc/7695073 to your computer and use it in GitHub Desktop.
Code in progress to address http://stackoverflow.com/questions/18611319/set-script-task-code-dynamically-in-ssis-2012
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------------------------------------------------- | |
// <copyright file="Driver.cs" company="billfellows.net"> | |
// I mention copyright so StyleCop is happy. | |
// </copyright> | |
//----------------------------------------------------------------------- | |
namespace SOCode | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.SqlServer.Dts; | |
// using Microsoft.SqlServer.SSIS.EzAPI; | |
using Microsoft.SqlServer.Dts.Runtime; | |
using Microsoft.SqlServer.Dts.Pipeline; | |
using Microsoft.SqlServer.Dts.Pipeline.Wrapper; | |
using Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask; | |
using Microsoft.SqlServer.Dts.Tasks.ScriptTask; | |
using Microsoft.SqlServer.VSTAHosting; | |
// C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SqlServer.IntegrationServices.VSTA\v4.0_11.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.IntegrationServices.VSTA.dll | |
using Microsoft.SqlServer.IntegrationServices.VSTA; | |
using System.Collections; | |
using System.Collections.Specialized; | |
/// <summary> | |
/// Driver class to demonstrate using EzAPI to build SSIS packages | |
/// </summary> | |
public class Driver | |
{ | |
/// <summary> | |
/// Where should these packages be written to | |
/// </summary> | |
public static string PackagePath = @"J:\Src\SO\SSIS"; | |
/// <summary> | |
/// A connection string to a database | |
/// </summary> | |
public static string ConnectionString = @"Data Source=localhost\DEV2012;Integrated Security=SSPI;Initial Catalog=TypeMoreClickLess;"; | |
public static void AddScriptTask() | |
{ | |
Application app = new Application(); | |
Package p = new Package(); | |
//VstaHelper scriptTaskHelper = new VstaHelper(); | |
bool supportDebug = true; | |
string fileName = "ScriptMain.vb"; | |
string language = "VisualBasic"; | |
string proj = ".vbproj"; | |
string code = string.Empty; | |
string codeName = string.Empty; | |
string configuration = "Debug"; | |
string moniker = string.Empty; | |
moniker = "STOCK:ScriptTask"; | |
Executable exScrPOC = p.Executables.Add(moniker); | |
TaskHost thOuter = exScrPOC as TaskHost; | |
TaskHost thInner = thOuter.InnerObject as TaskHost; | |
VSTAScriptProjectStorage storage; | |
thOuter.Name = "SCR magic"; | |
thOuter.Description = "Generated by magic"; | |
// Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptTask | |
Console.WriteLine(thOuter.InnerObject.ToString()); | |
ScriptTask scriptTask = thOuter.InnerObject as ScriptTask; | |
// This does not appear to be actually setting the language | |
// Perhaps because the code itself isn't being set? | |
scriptTask.ScriptLanguage = VSTAScriptLanguages.GetDisplayName(language); | |
scriptTask.ScriptingEngine.VstaHelper.Initalize("appName", supportDebug); | |
scriptTask.ScriptingEngine.VstaHelper.AddFileToProject(fileName, Driver.sourceCode); | |
scriptTask.ScriptingEngine.VstaHelper.Build(configuration); | |
storage = new VSTAScriptProjectStorage(); | |
scriptTask.ScriptingEngine.VstaHelper.SaveProjectToStorage(storage); | |
Console.WriteLine(scriptTask.ScriptingEngine.VstaHelper.TemplateFilePrefix); | |
// Logic for saving the package out | |
string outputFile = string.Empty; | |
string packageName = @"ScriptTask.dtsx"; | |
outputFile = string.Format(@"{0}\{1}", Driver.PackagePath, packageName); | |
app.SaveToXml(outputFile, p, null); | |
} | |
/* | |
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 to build vb script code: " + codeName); | |
scriptTask.ScriptingEngine.SaveScriptToStorage(); | |
if (!scriptTask.ScriptingEngine.CloseIDE(false)) | |
{ | |
throw new Exception("Unable to close Scripting engine."); | |
} | |
} | |
*/ | |
public static string sourceCode = @" | |
Imports System | |
Imports System.Data | |
Imports System.Math | |
Imports Microsoft.SqlServer.Dts.Runtime | |
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> _ | |
<System.CLSCompliantAttribute(False)> _ | |
Partial Public Class ScriptMain | |
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase | |
Public Sub Main() | |
Dts.TaskResult = ScriptResults.Success | |
End Sub | |
Enum ScriptResults | |
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success | |
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure | |
End Enum | |
End Class | |
"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment