I need to parse through all the user variables in my ssis packages. As of now, I am able to get the names and values of ALL the variables in my package. I need to grab the name and the value and dump them to a table. As of now, I can display the name and the value through a message box, but I cant seem to figure out, how on script task I would be able to dump these values to a table. Any help will be appreciated.
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
//using System;
////using Microsoft.SqlServer.Dts.Runtime;
namespace ST_81ec2398155247148a7dad513f3be99d.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
// Load a sample package that contains a variable that sets the file name.
Package pkg = app.LoadPackage(
#"C:\PackagePath\" +
#"Template0719.dtsx",
null);
Variables pkgVars = pkg.Variables;
foreach (Variable pkgVar in pkgVars)
{
MessageBox.Show(pkgVar.Name);
MessageBox.Show(pkgVar.Value.ToString());
}
Console.Read();
}
}
}
Since you know how to get the list of variables, you could create a data source script component.
Related
I have to build a job in SSIS to check if file with today's date exist then load data into targeted table.
My problem is I have different files stored with different dates in this folder with the format FileName_yyyyMMddhhmmss.csv and am not able to check if I have a filename with today's date (FileName_20220309) exist.
What I have done so far is I have created 3 variables
FolderPath
FileName
FileExistsFlg
For the variable FileName, I have used the following expression to get the format FileName_20220309
"Player_info_" + (DT_WSTR,50)(((DT_I8) ((DT_WSTR,4)DATEPART("yyyy",GetDate()) + RIGHT("0" +
(DT_WSTR,2)DATEPART("mm",GetDate()) ,2) +RIGHT("0" + (DT_WSTR,2)DATEPART("dd",GetDate()),2))))
I have used a Script Task component where I have passed variables FileName and FolderPath as ReadOnlyVariables and FileExistsFlg as ReadWriteVariables
Below is my script used in the script task component
#region Namespaces
using System;
using System.IO;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
#endregion
namespace ST_3692973debdd4531ac4eced28213e38f
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain :
Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
// TODO: Add your code here
String Filepath = Dts.Variables["User::FolderPath"].Value.ToString()+Dts.Variables["User::FileName"].Value.ToString();
String SearchString = Dts.Variables["User::FileName"].Value.ToString();
if(
File.Exists(Filepath))
{
Dts.Variables["User::FileExistsFlg"].Value = 1;
}
MessageBox.Show(Filepath);
MessageBox.Show(Dts.Variables["User::FileExistsFlg"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
}
}
With the above code, I'm not able to check if a file with today's date (FileName_20220309) exists.
Can someone help with how I can modify the above code to check if the string exists in the filename?
I think you want something like this...
var files = DirectoryInfo(folderPath).GetFiles("*" + DateTime.Now.ToString("yyyyMMdd") + "*");
bool flag = files.Length > 0 ? true : false;
This will identify any files in a folder that have today's date in the yyyyMMdd format in them.
Using File.Exists(Filepath) need a complete path of the file instead of the file name. For example, File.Exists(#"C:\data_2022.csv");. Instead of using the FileExists() functions, you should enumerate the directory files and check if a file with the given name exists.
Consider using the following code:
#region Namespaces
using System;
using System.IO;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
#endregion
namespace ST_3692973debdd4531ac4eced28213e38f
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain :
Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
// TODO: Add your code here
String Filepath = Dts.Variables["User::FolderPath"].Value.ToString()+Dts.Variables["User::FileName"].Value.ToString();
String SearchString = Dts.Variables["User::FileName"].Value.ToString();
if(Directory.GetFiles(Filepath,SearchString,SearchOption.AllDirectories).length > 0)
{
Dts.Variables["User::FileExistsFlg"].Value = 1;
}
MessageBox.Show(Filepath);
MessageBox.Show(Dts.Variables["User::FileExistsFlg"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
}
}
I am trying to access the macros inside of an Access database (accdb).
I tried using:
using Microsoft.Office.Interop.Access.Dao;
...
DBEngine dbe = new DBEngine();
Database ac = dbe.OpenDatabase(fileName);
I found a container["Scripts"] that had a document["Macro1"] which is my target. I am struggling to access the contents of the document. I also question if the Microsoft.Office.Interop.Access.Dao is the best reference for what I am trying to achieve.
What is the best way to view the content of the macros and modules?
You can skip the DAO part, it's not needed in this case. Macros are project specific, so in order to get them all, you would need to loop through your projects. In my example, i just have one project.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Access;
namespace Sandbox48
{
public class Program
{
public static void Main(string[] args)
{
Microsoft.Office.Interop.Access.Application oAccess = null;
string savePath = #"C:\macros\";
oAccess = new Microsoft.Office.Interop.Access.Application();
// Open a database in exclusive mode:
oAccess.OpenCurrentDatabase(
#"", //filepath
true //Exclusive
);
var allMacros = oAccess.CurrentProject.AllMacros;
foreach(var macro in allMacros)
{
var fullMacro = (AccessObject)macro;
Console.WriteLine(fullMacro.Name);
oAccess.SaveAsText(AcObjectType.acMacro, fullMacro.FullName, $"{savePath}{ fullMacro.Name}.txt");
}
Console.Read();
}
}
}
I have a fairly simple requirement to read the filename from a location and populate a variable with the filename. I am using a script task for this task. No matter what I try, the value of the filename is not populating in the variable.
I'm using a Script task Editor. There is only one ReadWriteVariables which will hold the full name of the file.
Here is the script that I'm using:
namespace ST_f8fd828f11b64932b15f2681e86c8d94
{
using System;
using System.Data;
using System.IO;
using Microsoft.SqlServer.Dts.Runtime;
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
string p = "C:\\Users\\nthaku01\\Desktop\\NewEXPOLD.txt";
FileInfo fi = new FileInfo(p);
String fileName = fi.FullName;
Dts.Variables["User::vLastFilename"].Value = fileName.ToString();
MessageBox.Show(Dts.Variables["User::vLastFilename"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
The MessageBox displays the name(and path) of the file. However, the variable is empty.
This issue has been discussed in detail previously as well and I tried every trick. However, it is just not working for me. Please help!!
Thanks,
Navneet
The only problem with your script task that I could see is that ScriptResults was already defined for the script. I renamed your enum section and the enumerators, and I have no problem using the variable to insert the text into a db table. Here's the script task I used:
public void Main()
{
string p = "C:\\Users\\nthaku01\\Desktop\\NewEXPOLD.txt";
FileInfo fi = new FileInfo(p);
String fileName = fi.FullName;
Dts.Variables["User::vLastFilename"].Value = fileName.ToString();
MessageBox.Show(Dts.Variables["User::vLastFilename"].Value.ToString());
Dts.TaskResult = (int)eScriptResults.enumSuccess;
}
enum eScriptResults
{
enumSuccess = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
enumFailure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
I got it resolved.
Actually, as an SSIS newbie, I was not aware that the value is assigned to the variable during the run-time does not show up in the variable window.
In the variable window, only the value mentioned at the design time shows up. I had to create a derived column and I assigned the value of the user variable to this newly created derived column. This resolved the issue I was facing.
In the Derived column transformation:
output_LastFileName <-- User::vLastFilename
I have been using edge.js to call a C# function from within my Node.js app, however when I go to execute the C# code I get for example:
Metadata file 'System.Collections.Generic.dll' could not be found
Metadata file 'System.Text.dll' could not be found
...
My code is this below, basically wanting to run a SSIS package using a stored procedure which I am calling from C#. Basically all my referenced dll's can't be found? Where should I put the dlls for edge to find them?
var executeSQL = edge.func(function() {
/*
#r "System.Data.dll"
#r "System.Collections.Generic.dll"
#r "System.Linq.dll"
#r "System.Text.dll"
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Threading.Tasks;
public class StartUp
{
public async Task<object> Invoke(object input)
{
string result = string.Empty;
string packagePath = #"\SSISDB\test\package.dtsx";
string spName = "storedProcName";
using (var conn = new System.Data.SqlClient.SqlConnection("connectionString"))
using (var command = new System.Data.SqlClient.SqlCommand(spName, conn)
{
CommandType = System.Data.CommandType.StoredProcedure
})
{
conn.Open();
command.Parameters.AddWithValue("#PackagePath", packagePath);
command.ExecuteNonQuery();
Console.WriteLine("Finished");
};
return null;
}
}
*/
});
I know I can do this without C# and just use a module within node like mssql to execute the stored procedure but this was just an example test to get used to using edge.js
The comment from stuartd was correct in the sense to put the dlls under the same directory as the script (which I had tried) but I was still having the same issue. I solved my problem by having my C# code as a separate file and then referenced that file as below as part of the executeSSIS function. payload is just the object that gets passed from my node.js script to my C# script. Doing it this way solved my issue.
var payload = {
filePath: 'C:/temp/xlsx/' + req.file.filename,
path: req.packageName,
server: req.server
};
var executeSSIS = edge.func({
source: __dirname + '/cs/Program.cs',
references: [
__dirname + '/cs/System.Data.dll'
]
});
executeSSIS(payload);
I am creating an SSIS package and want to include a script which checks if a file exist before retrieving the file and saving that data to a table.
I have three separate variable that I have set up:
fileExistFlag Int32 0
fileName String check.txt
folderPath String C:\
My C# code looks like this, where I am checking:
public void Main()
{
// TODO: Add your code here
String fp = Dts.Variables["User::folderPath"].Value.ToString() + Dts.Variables["User::fileName"].Value.ToString();
if (File.Exists(fp))
{
Dts.Variables["User::fileExistFlag"].Value = 1;
}
MessageBox.Show(fp);
MessageBox.Show(Dts.Variables["User::fileExistFlag"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
When I try to compile my script, I receive the following error:
Cannot apply indexing with [] to an expression of type 'Microsoft.SqlServer.Dts.Runtime.Variables for all four instances.
How can I solve the issue?
Updated code:
/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
namespace ST_04f6fa3ba49a4ddeac3d3d7fc29f04f2.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
/*
The execution engine calls this method when the task executes.
To access the object model, use the Dts property. Connections, variables, events,
and logging features are available as members of the Dts property as shown in the following examples.
To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
To post a log entry, call Dts.Log("This is my log text", 999, null);
To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);
To use the connections collection use something like the following:
ConnectionManager cm = Dts.Connections.Add("OLEDB");
cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";
Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
To open Help, press F1.
*/
public void Main()
{
// TODO: Add your code here
String fp = Dts.Variables.Get("User::folderPath").Value.ToString() + Dts.Variables.Get("User::fileName").Value.ToString();
if (File.Exists(fp))
{
Dts.Variables.Get("User::fileExistFlag").Value = 1;
}
MessageBox.Show(fp);
MessageBox.Show(Dts.Variables.Get("User::fileExistFlag").Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
}
public static Microsoft.SqlServer.Dts.Runtime.Variable Get(
this Microsoft.SqlServer.Dts.Runtime.Variables variables, string name)
{
foreach(Microsoft.SqlServer.Dts.Runtime.Variable item in variables)
{
if(item.Name == name) return item;
}
return null;
}
}
This is a known BUG in SQL Server BIDS 2005/2008 after installing side by side a later version of SSIS. For example if you are developing a SSIS 2008 package and then install SSIS 2012.
A workaround is to move the file "Microsoft.SQLServer.ManagedDTS.dll" located in the path:
"C:\Program Files (x86)\Microsoft SQL Server\110\SDK\Assemblies" to a backup folder, then the bids take the reference from the path "C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.ManagedDTS\10.0.0.0__89845dcd8080cc91\"
But it doesn't seem to work for all the cases reported.
Source:
https://connect.microsoft.com/SQLServer/feedback/details/744390/ssis-any-pre-2012-error-cannot-apply-indexing-with-to-an-expression-of-type-microsoft-sqlserver-dts-runtime-variables
http://support.microsoft.com/kb/938608/en-us
Oddly, this indexer does seem to exist. If it isn't working, though, you might be able to use an extension method:
public static class MyExtensionMethods
{
public static Microsoft.SqlServer.Dts.Runtime.Variable Get(
this Microsoft.SqlServer.Dts.Runtime.Variables variables, string name)
{
foreach(Microsoft.SqlServer.Dts.Runtime.Variable item in variables)
{
if(item.Name == name) return item;
}
return null;
}
}
and use:
... Dts.Variables.Get("User::folderPath").Value ...
instead.
Use Browse in the add references window and look for this dll: C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SQLServer.ManagedDTS.dll