Delete Rows Start in CSV file In SSIS Controlflow Script Task - c#

I have a .csv file that looks like this:
#Example Company
#(999) 999-9999
#http://yourwebsite.com
#Report Date Range: Dec 26, 2013 - Dec 26, 2013
#Exported: Dec 26, 2013
#Twitter : Profile Summary
#Screen Name,Name,Description,Location,Followers,Following,Listed
SctaSa,statisticalgraph,statistical Screen- The official account for your
organization,Saudi Arabia,6775,8,75
So, I need to take specific data from the .csv file to be readable to SSIS Transformation, start from column "Screen Name" and remove the garbage data which start with # , to be look like that
Screen Name,Name,Description,Location,Followers,Following,Listed,Exported,Report Date Range
SctaSa,statisticalgraph,statistical Screen- The official account for your organization,Saudi Arabia,6775,8,75,26-Dec-13,26-Dec-13
i tried to use this C# script but it does not wore file (I'm not an expert in C# so I don't know what the problem is) I tried to use the following script to delete any line start with # but the file dose not transfare to the out put path; could you give me any suggestions?!
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
#endregion
namespace ST_a7b941606e0b40aa920bfe13fc81dc81
{
/// <summary>
/// ScriptMain is the entry point class of the script. Do not change the name, attributes,
/// or parent of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
protected void Page_Load(object sender, EventArgs e)
{
var lines = new List<string>();
string line;
using (var file = new System.IO.StreamReader("D:\\try.csv"))
{
while ((line = file.ReadLine()) != null)
{
if (line.Length != 0)
{
if (!line.StartsWith("#") )
{
lines.Add(line);
}
}
}
}
File.WriteAllLines("D:\\SCTA_ETL\\try.csv", lines);
}
/// <summary>
/// This method is called when this script task executes in the control flow.
/// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
/// To open Help, press F1.
/// </summary>
public void Main()
{
// TODO: Add your code here
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}

Another way:
File.WriteAllLines(outputPath, File.ReadAllLines("c:\\mycsv.csv").Where(x => !x.StartsWith("#")).ToArray());

You might want to change your logic in the middle:
var lines = new List<string>();
string outputPath = // your output path here
using (var file = new System.IO.StreamReader("c:\\mycsv.csv"))
{
string line;
while ((line = file.ReadLine()) != null)
{
if (!line.StartsWith("#"))
{
lines.Add(line);
}
}
}
File.WriteAllLines(outputPath, lines);
You had been removing all lines that had "#" anywhere inside.
Instead, only add in lines that do not start with "#".
Also, be sure to close and dispose your StreamReader when you are done with it, or just put the whole thing in using section.

Related

Loading Most Recent Excel File with SSIS: Script Task Debugging

I try to get the "Last modified" excel file in a folder and load it in SSIS. I found a C# code to get the name of most recent excel sheet in a folder path, and copy that in the Script Task. The code is :
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
namespace ST_2e01f076aa4f46d692cf4b47f5587da9.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()
{
// TODO: Add your code here
var directory = new DirectoryInfo(Dts.Variables["User::VarFolderPath"].Value.ToString());
FileInfo[] files = directory.GetFiles();
DateTime lastModified = DateTime.MinValue;
foreach (FileInfo file in files)
{
if (file.LastWriteTime > lastModified)
{
lastModified = file.LastWriteTime;
Dts.Variables["User::VarFileName"].Value = file.ToString();
}
}
MessageBox.Show(Dts.Variables["User::VarFileName"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
However, when I run the script task to test it, I get the following error:
I used the project name displaying in error in my code, but still does not work. Could you please kindly help me how to fix it as I am new to both SSIS and C#. Thanks
Here is an answer using Linq.
First add these namespaces
using System.Collections.Generic; //This gets you list
using System.Linq; //This allows you linq functions
//Here is your code
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(#"D:\Temp");
List<System.IO.FileInfo> fi = di.EnumerateFiles().ToList();
Dts.Variables["VarFileName"].Value = fi.Where(i=>i.Extension.ToLower()==".xls")
.OrderByDescending(i => i.LastWriteTime)
.Select(i => i.FullName).FirstOrDefault();

run time error in SAP Business One Client

My code has runtime error
Error:
An exception of type 'System.Runtime.InteropServices.COMException' occurred in MatrixFill.exe but was not handled in user code
Additional information: 1). [Microsoft] [SQL Server Native Client 11.0] [SQL Server] Incorrect syntax near 'OCRD'.
I want to connect to sap via c # and fill the matrix
How do I change the code ???
my code:
using System;
using System.Collections.Generic;
using System.Xml;
using SAPbouiCOM.Framework;
namespace MatrixFill
{
[FormAttribute("MatrixFill.Form1", "Form1.b1f")]
class Form1 : UserFormBase
{
public Form1()
{
}
/// <summary>
/// Initialize components. Called by framework after form created.
/// </summary>
public override void OnInitializeComponent()
{
this.Matrix0 = ((SAPbouiCOM.Matrix)(this.GetItem("Item_0").Specific));
this.Button0 = ((SAPbouiCOM.Button)(this.GetItem("btnFill").Specific));
this.Button0.ClickBefore += new
SAPbouiCOM._IButtonEvents_ClickBeforeEventHandler(this.Button0_ClickBefore);
this.OnCustomInitialize();
}
/// <summary>
/// Initialize form event. Called by framework before form creation.
/// </summary>
public override void OnInitializeFormEvents()
{
}
private SAPbouiCOM.Matrix Matrix0;
private void OnCustomInitialize()
{
}
private SAPbouiCOM.Button Button0;
private void Button0_ClickBefore(object sboObject, SAPbouiCOM.SBOItemEventArg pVal, out bool BubbleEvent)
{
BubbleEvent = true;
SAPbobsCOM.Company oCompany = (SAPbobsCOM.Company)Application.SBO_Application.Company.GetDICompany();
SAPbobsCOM.Recordset oRset = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
string Query = "select CardCode,CardName,E_Mail sFrom OCRD";
oRset.DoQuery(Query);
if (oRset.RecordCount > 0)
{
for (int i = 0; i < oRset.RecordCount; i++)
{
Matrix0.AddRow();
((SAPbouiCOM.EditText)Matrix0.Columns.Item("colCode").Cells.Item(i + 1).Specific).Value = oRset.Fields.Item("CardCode").Value.ToString();
((SAPbouiCOM.EditText)Matrix0.Columns.Item("colName").Cells.Item(i + 1).Specific).Value = oRset.Fields.Item("CardName").Value.ToString();
((SAPbouiCOM.EditText)Matrix0.Columns.Item("colEmail").Cells.Item(i + 1).Specific).Value = oRset.Fields.Item("E_Mail").Value.ToString();
oRset.MoveNext();
}
}
}
}
}
note:
My form has a matrix with three columns
The matrix is designed in Visual Studio 2015
It's because you have the s just before From near the bottom.
Change this
select CardCode,CardName,E_Mail sFrom OCRD
to
select CardCode,CardName,E_Mail From OCRD
Cheers
Jon

Affect SSIS Object variable in a C# Script Task

I've created a simple script that I can't debug. Here is my issue :
I'm looking to store the content of a directory into a variable in SSIS with Visual Studio 2015.
I've created a variable in my SSIS package, and set it's data type to Object.
I've added to my package a Script Task, that contains this code :
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
#endregion
namespace ST_c6399821104c42c2859b7b2481055848 {
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase {
public void Main() {
string CSVFilesCompletePath;
if (Dts.Variables.Contains("User::CSVFilesPathAbsolute") == true
&& Dts.Variables.Contains("User::CSVFilesPathRelativeCountry") == true
&& Dts.Variables.Contains("User::CSVFilesCountryObject") == true) {
CSVFilesCompletePath = Dts.Variables["User::CSVFilesPathAbsolute"].ToString() + Dts.Variables["User::CSVFilesPathRelativeCountry"].ToString();
String[] fileListTable = Directory.GetFiles(CSVFilesCompletePath, "*.xlsx");
List<string> fileList = new List<string>(fileListTable);
Dts.Variables["User::CSVFilesCountryObject"].Value = fileList;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
As explained here :
SSIS Script Task Get File Names and Store to an SSIS Object Variable
But this code returns the following error when I try to Start it through the SSIS Job :
Variables are correctly set in the Script Wizard as ReadWriteVariables.
The thing is that my code shows this error when I try to affect the SSIS Variable and try to put the String[] in it.
Try adding a try-catch in the
Directory.GetFiles
call or in the whole body, then set the error message and/or the stack trace of the exception into a write SSIS variable for debugging purposes.
Edit: Looks like #H.Fadlallah point out the problem. You should use the Value property of the DtsVariable, not the ToString()
My error was :
CSVFilesCompletePath = Dts.Variables["User::CSVFilesPathAbsolute"].ToString() + Dts.Variables["User::CSVFilesPathRelativeCountry"].ToString();
I had to use : Dts.Variables[].Value.ToString() instead of Dts.Variables[].ToString()
As I was using the name of the Variable instead of the content of the Variable, the GetFiles returned a null object, and it couldn't be stored inside my variable, creating the different errors.

Retrieving last commit ID / revision number from a deployed application

I would like to have a function returning the last revision / commit ID of the source from which the deployed application / library was built.
I have been using the __DATE__ and __TIME__ macros in C++ but this approach has obvious drawbacks.
What tools are available to achieve this in an automated manner?
I am using Eclipse-CDT for C++ (Linux, SVN) but it I am also interested in git solutions, and sources written in Java and C#.
By using Visual Studio and C# you could within a Pre-Built step calling the SubWCRev command with a template file, that will be copied to a file used within the solution.
The command within the Pre-Built step is:
<PreBuildEvent>SubWCRev "$(ProjectDir)\" "$(ProjectDir)VersionProvider.template.cs" "$(ProjectDir)VersionProvider.cs"</PreBuildEvent>
Within the project add the following two files:
<Compile Include="VersionProvider.cs">
<DependentUpon>VersionProvider.template.cs</DependentUpon>
</Compile>
<None Include="VersionProvider.template.cs" />
With this content:
internal static class VersionProvider
{
/// <summary>
/// Provides the current subversion revision number
/// </summary>
internal const string CurrentSVNRevision = "$WCREV$";
}
Last but not least within AssemblyInfo.cs add the following line:
[assembly: AssemblyInformationalVersion(VersionProvider.CurrentSVNRevision)]
The project will by this way automatically get the current subversion revision number of this project folder baked into version info of the application which can be seen on the details page of the file properties.
You can also retrieve this information through code at runtime:
private string GetAdditionalVersionInfo()
{
var assembly = Assembly.GetEntryAssembly();
var attributesFound = assembly.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), true);
var version = attributesFound.OfType<AssemblyInformationalVersionAttribute>().FirstOrDefault();
return version != null ? version.InformationalVersion : String.Empty;
}
/// <summary>
/// Gets the SVN date.
/// </summary>
/// <value>The SVN date. value</value>
// ReSharper disable UnusedMember.Global
public static new string SvnDate{
// ReSharper restore UnusedMember.Global
get {
const string S = "$Date$";
return S.Substring(7, 19);
}
}
/// <summary>
/// Gets the SVN ID.
/// </summary>
/// <value>The SVN ID. value</value>
// ReSharper disable UnusedMember.Global
public static new string SvnID{
// ReSharper restore UnusedMember.Global
get{
const string S = "$Id$";
const string D = "$";
return S.Replace(D + "Id: ", string.Empty).Replace(" " + D, string.Empty);
}
}
/// <summary>
/// Gets the SVN rev.
/// </summary>
/// <value>The SVN rev. value</value>
// ReSharper disable UnusedMember.Global
// ReSharper disable MemberCanBePrivate.Global
public static new string SvnRev{
// ReSharper restore MemberCanBePrivate.Global
// ReSharper restore UnusedMember.Global
get {
const string S = "$Rev$";
return S.Item(1);
}
}
/// <summary>
/// Gets the SVN author.
/// </summary>
/// <value>The SVN author. value</value>
// ReSharper disable UnusedMember.Global
public static new string SvnAuthor{
// ReSharper restore UnusedMember.Global
get {
const string S = "$Author$";
return S.Item(1);
}
}
Note S.Item(n) returns a space delimited zero based item. Replace with your own code. Also, new is only necessary to deal with inheritance. Props must be set in svn. I dont have a snippet showing the expansions since I am on Mercurial now.
If you were working with a Makefile and SVN, you could do something like this for C or C++:
REVISION=$(shell svnversion)
CFLAGS=... -D__REVISION__=\"$REVISION\"
I'm not that familiar with CDT, but I understand it's able to use a Makefile-based build system.

Display a .exe.config file in a datagridview and manipulate the settings

I'm working on a GUI that is able to manipulate xml files through a datagridview and have it saved to a destination of the user's choice. This program also has a .exe.config file in which I would also like to be able to freely edit inside a datagridview, since it's a lot more convenient than having the user manually going in to the file and changing the values accordingly.
I've tried declaring a dataset, and I intially thought that a .exe.config file was just an xml file, but this code does not work:
dataSet1.ReadXml(configpath);
bindingSource1.DataSource = dataSet1.Tables[0];
dataGridView1.DataSource = bindingSource1;
The datagridview is empty when I ran it and i confirmed that the filepath was correct and there was no exception when i debugged the code, whereas for the other xml files I open in the GUI work perfectly fine with the data displayed. Maybe readxml() only supports.. legit xml files rather than xml configuration files? I tried googling and looking for some answers, but all I got were threads related to changing the settings by manually accessing the xml file and changing the values (stuff I already know). I'm looking to be able to have the user do what they want to do with the data and then save it. The .exe.config settings may just as well be for another program, but it is essentially an xml configuration file. I figured there wasn't much on the web for this particular problem because settings are generally static and if they are changed, it's pretty easy to do manually.
To sum up,
I'm looking for a method to be able to open any .exe.config file, display it in a datagridview, allow the user to be able to manipulate the data values inside, and then save the file, overwriting the previous data settings.
Any help is appreciated.
Thank you in advance!
tf.rz (.NET 3.5 SP1, Visual Studio 2008 C#)
EDIT: I will upload a working example of an xml file I created: I kind of want the program to be able to navigate to a .exe.config file, then open it and have it displayed like this where the setting names are the columns and the values are in the cells of the datagridview. Unfortunately I am not at my home computer to be able to do this.
This is what I used to load up and manipulate a config file. You may want to change the loadAppSettings and loadConnStrings methods to suit your needs.
using System;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace GenericManagementClasses
{
public class ConfigFile
{
private string m_ConfigFilePath;
private XmlDocument m_XmlDoc;
private FileStream fIn;
private StreamReader sr;
private StreamWriter sw;
private OrderedDictionary m_AppSettings;
private OrderedDictionary m_ConnectionStrings;
private XmlNode m_AppSettingsNode;
private XmlNode m_ConnectionStringsNode;
#region "Properties"
public String Path
{
get
{
return m_ConfigFilePath;
}
}
public OrderedDictionary AppSettings
{
get
{
return m_AppSettings;
}
}
public OrderedDictionary ConnectionStrings
{
get
{
return m_ConnectionStrings;
}
}
#endregion
#region "Constructors"
/// <summary>
/// Default constructor - declared private so that you can't instantiate an empty ConfigFile object
/// <code>ConfigFile cfg = new ConfigFile()</code> will result in a NotImplemented exception
/// </summary>
private ConfigFile()
{
throw new NotImplementedException("No default constructor for the ConfigFile class");
}
/// <summary>
/// Public constructor
/// <example>ConfigFile cfg = new ConfigFile(#"c:\MyApp\MyApp.exe.config");</example>
/// </summary>
/// <param name="ConfigFilePath">The path to the configuration file</param>
public ConfigFile(string ConfigFilePath)
{
//Check to see if the file exists
if (File.Exists(ConfigFilePath)){
//Initialise the XmlDocument to hold the config file
m_XmlDoc = new XmlDocument();
//Store the path to the config file
m_ConfigFilePath = ConfigFilePath;
//FileStream to get the contents out of the file
fIn = new FileStream(m_ConfigFilePath, FileMode.Open, FileAccess.ReadWrite);
//StreamReader to read the FileStream
sr = new StreamReader(fIn);
//StreamWriter to write to the FileStream
sw = new StreamWriter(fIn);
//Try and load the XML from the file stream
try
{
m_XmlDoc.LoadXml(sr.ReadToEnd());
m_AppSettingsNode = m_XmlDoc.GetElementsByTagName("appSettings")[0];
m_ConnectionStringsNode = m_XmlDoc.GetElementsByTagName("connectionStrings")[0];
loadAppSettings();
loadConnStrings();
}
catch (Exception ex)
{
//If it went pear shaped, throw the exception upwards
throw ex;
}
}
else
//If the file doesn't exist, throw a FileNotFound exception
{
throw new FileNotFoundException(ConfigFilePath + " does not exist");
}
}
#endregion
private void loadAppSettings()
{
m_AppSettings = new OrderedDictionary();
XmlNodeList nl = m_AppSettingsNode.SelectNodes("add");
foreach (XmlNode node in nl)
{
m_AppSettings.Add(node.Attributes["key"].Value, node.Attributes["value"].Value);
}
}
private void loadConnStrings()
{
m_ConnectionStrings = new OrderedDictionary();
XmlNodeList nl = m_ConnectionStringsNode.SelectNodes("add");
foreach (XmlNode node in nl)
{
m_ConnectionStrings.Add(node.Attributes["name"].Value, node.Attributes["connectionString"].Value);
}
}
public void setAppSetting(string name, string newValue)
{
if (!m_AppSettings.Contains(name))
{
throw new Exception(String.Format("Setting {0} does not exist in {1}", name, m_ConfigFilePath));
}
else
{
m_AppSettings[name] = newValue;
m_XmlDoc.SelectSingleNode(String.Format(#"//appSettings/add[#key='{0}']",name)).Attributes["value"].Value = newValue;
fIn.SetLength(0);
sw.Write(m_XmlDoc.InnerXml);
sw.Flush();
}
}
#region "Static Methods"
/// <summary>
/// Static method to return a ConfigFile object
/// <example>ConfigFile cfg = ConfigFile.LoadConfigFile(#c:\MyApp\MyApp.exe.config");"</example>
/// </summary>
/// <param name="ConfigFilePath">Path to the configuration file to load</param>
/// <returns></returns>
public static ConfigFile LoadConfigFile(string ConfigFilePath)
{
return new ConfigFile(ConfigFilePath);
}
#endregion
}
}

Categories

Resources