<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tim laqua dot com &#187; c#</title>
	<atom:link href="http://timlaqua.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://timlaqua.com</link>
	<description>Thoughts and Code from Tim Laqua</description>
	<lastBuildDate>Fri, 16 Mar 2012 16:48:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Consuming an Authenticated JSON Feed with SSIS</title>
		<link>http://timlaqua.com/2011/07/consuming-an-authenticated-json-feed-with-ssis/</link>
		<comments>http://timlaqua.com/2011/07/consuming-an-authenticated-json-feed-with-ssis/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 14:53:55 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[ssis]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=656</guid>
		<description><![CDATA[For this post, we'll look in to one option for consuming a JSON data feed from a service provider's API. In my case, the provider is AtTask - a web based project management service. This sort of assignment/request is pretty common as various corners of your organization simply want to use the best tool for [...]]]></description>
			<content:encoded><![CDATA[<p>For this post, we'll look in to one option for consuming a JSON data feed from a service provider's API.  In my case, the provider is <a href="http?://www.attask.com/">AtTask</a> - a web based project management service.  This sort of assignment/request is pretty common as various corners of your organization simply want to use the best tool for the job.  Far too infrequently does anyone really look at things like how it connects to your current systems.  They say things like "we have a well documented API and a strong user community" - yeah, I've heard that a few thousand times.  JSON is neat if you're using JQuery.  It's horrible if you're trying to suck data down in to a SQL Server table.  Which is what we're going to do.</p>
<p>First, we wander over to the <a href="https://community.attask.com/attask-restful-api">AtTask API Documentation</a> and figure out how to authenticate.  Notice that every request needs a sessionID - and that sessionID is created by a request to the login url.  This means we'll have to make at least two requests - one to nab a sessionID and another to actually get the data that we want.<br />
<span id="more-656"></span><br />
First, create a new package and drop down all the basic objects we'll need:</p>
<ol>
<li>OLEDB Connection</li>
<li>SQL Task: BEGIN TRANSACTION</li>
<li>SQL Task: TRUNCATE (truncate target table)</li>
<li>Data Flow: Populate table</li>
<li>SQL Task: COMMIT TRANSACTION</li>
<li>SQL Task: ROLLBACK TRANSACTION (failure predicates from truncate and data flow)</li>
</ol>
<p><em>Suggested Control Flow</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/ControlFlow_Complete.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/ControlFlow_Complete.png" alt="" title="ControlFlow_Complete" width="380" height="304" class="alignnone size-full wp-image-685" /></a></p>
<p>Because we're trying to maintain a single transaction through multiple executables, set the <strong>RetainSameConnection </strong>property of your OLEDB connection to True.  You certainly could just TRUNCATE and then run the data flow, I just wanted to avoid having the end user(s) hit an empty table mid-load (the API I was working with was a tad slow at times).</p>
<p>Now that you have the control flow under... control... jump in to the Data flow where the interesting stuff happens.  You'll need at least a Script Component Source and an OLEDB destination.</p>
<p><em>Suggested Data Flow</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/DataFlow_Complete.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/DataFlow_Complete.png" alt="" title="DataFlow_Complete" width="148" height="142" class="alignnone size-full wp-image-686" /></a></p>
<p>I created two variables to hold the URLs I needed to both login to the API and make the actual request<br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/DataFlow_Variables.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/DataFlow_Variables.png" alt="" title="DataFlow_Variables" width="369" height="62" class="alignnone size-full wp-image-687" /></a></p>
<p>Open up the Script Component Source and add the variables to the component:<br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/ScriptComponent_ReadOnlyVariables.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/ScriptComponent_ReadOnlyVariables.png" alt="" title="ScriptComponent_ReadOnlyVariables" width="461" height="50" class="alignnone size-full wp-image-691" /></a></p>
<p>Then configure the output to match what you want to come out of the component:<br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/ScriptComponent_AddOutputColumns.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/ScriptComponent_AddOutputColumns.png" alt="" title="ScriptComponent_AddOutputColumns" width="497" height="349" class="alignnone size-full wp-image-690" /></a></p>
<p>Finally, edit the script and switch it to build for the .NET Framework 3.5:<br />
<em>Project Explorer > Properties</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/ProjectExplorer_Properties.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/ProjectExplorer_Properties.png" alt="" title="ProjectExplorer_Properties" width="277" height="299" class="alignnone size-full wp-image-689" /></a></p>
<p><em>Select .NET 3.5</em><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/SelectDotNetFramework35.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/SelectDotNetFramework35.png" alt="" title="SelectDotNetFramework35" width="384" height="195" class="alignnone size-full wp-image-692" /></a></p>
<p>Then Add references that we'll need for this script:<br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/ProjectExplorer_AddReference.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/ProjectExplorer_AddReference.png" alt="" title="ProjectExplorer_AddReference" width="292" height="173" class="alignnone size-full wp-image-688" /></a></p>
<p>Select Assemblies we'll need (System.Runtime.Serialization and System.ServiceModel.Web)<br />
<a href="http://timlaqua.com/wp-content/uploads/2011/07/AddReference_SelectAssemblies.png"><img src="http://timlaqua.com/wp-content/uploads/2011/07/AddReference_SelectAssemblies.png" alt="" title="AddReference_SelectAssemblies" width="464" height="382" class="alignnone size-full wp-image-684" /></a></p>
<p><strong>Make sure to Save All at this point!  You want to make sure that the changes you made to the project get saved, not just the changes to the main.cs file.</strong></p>
<p>And here's my completed script.  The specifics of how you need to login and what the returned data feeds look like will vary, but the basic flow will likely remain the same.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Pipeline.Wrapper</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Runtime.Wrapper</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Xml</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.Serialization.Json</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.Serialization</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text.RegularExpressions</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>Microsoft.<span style="color: #0000FF;">SqlServer</span>.<span style="color: #0000FF;">Dts</span>.<span style="color: #0000FF;">Pipeline</span>.<span style="color: #0000FF;">SSISScriptComponentEntryPointAttribute</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ScriptMain <span style="color: #008000;">:</span> UserComponent
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> CreateNewOutputRows<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// loginURL should be a url that returns JSON containing a sessionID</span>
        <span style="color: #FF0000;">string</span> loginURL <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">loginURL</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Get the sessionID</span>
        HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>loginURL<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        request.<span style="color: #0000FF;">Timeout</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">300000</span><span style="color: #008000;">;</span>
        HttpWebResponse response <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebResponse<span style="color: #000000;">&#41;</span>request.<span style="color: #0000FF;">GetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        StreamReader reader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>response.<span style="color: #0000FF;">GetResponseStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">string</span> json <span style="color: #008000;">=</span> reader.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Match m <span style="color: #008000;">=</span> Regex.<span style="color: #0000FF;">Match</span><span style="color: #000000;">&#40;</span>json, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>sessionID<span style="color: #008080; font-weight: bold;">\&quot;</span>: <span style="color: #008080; font-weight: bold;">\&quot;</span>(.+?)<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">string</span> sessionId <span style="color: #008000;">=</span> m.<span style="color: #0000FF;">Groups</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Need to support pagination, api has a 2000 item limit per req</span>
        XmlNodeList items<span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> rowsPerPage <span style="color: #008000;">=</span> <span style="color: #FF0000;">1000</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> page <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Loop until we find the last page</span>
        <span style="color: #0600FF;">do</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// The taskURL has 3 tokens:</span>
            <span style="color: #008080; font-style: italic;">// {0} = The first record to return for the page we're requesting</span>
            <span style="color: #008080; font-style: italic;">// {1} = The number of records to request</span>
            <span style="color: #008080; font-style: italic;">// {2} = The sessionID</span>
            <span style="color: #FF0000;">string</span> taskURL <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span>
                Variables.<span style="color: #0000FF;">taskURL</span>, 
                page <span style="color: #008000;">*</span> rowsPerPage <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span>, 
                rowsPerPage, 
                sessionId<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            request <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>taskURL<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            response <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebResponse<span style="color: #000000;">&#41;</span>request.<span style="color: #0000FF;">GetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Toss the JSON response in to XML so we can work with it better</span>
            XmlDocument xd <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlDocument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            XmlDictionaryReader xr <span style="color: #008000;">=</span> JsonReaderWriterFactory.<span style="color: #0000FF;">CreateJsonReader</span><span style="color: #000000;">&#40;</span>
                response.<span style="color: #0000FF;">GetResponseStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, 
                XmlDictionaryReaderQuotas.<span style="color: #0000FF;">Max</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            xr.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            xd.<span style="color: #0000FF;">LoadXml</span><span style="color: #000000;">&#40;</span>xr.<span style="color: #0000FF;">ReadOuterXml</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Put each item returned in to a new Buffer row</span>
            items <span style="color: #008000;">=</span> xd.<span style="color: #0000FF;">DocumentElement</span>.<span style="color: #0000FF;">SelectNodes</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;/root/data/item&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>XmlNode item <span style="color: #0600FF;">in</span> items<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                TaskDataBuffer.<span style="color: #0000FF;">AddRow</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">ProjectId</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;project/ID&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">ProjectName</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;project/name&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// This particular feed allows assignedTo to be empty/null</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;assignedTo/name&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    TaskDataBuffer.<span style="color: #0000FF;">TaskAssignedToName</span> <span style="color: #008000;">=</span> 
                        item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;assignedTo/name&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">else</span>
                <span style="color: #000000;">&#123;</span>
                    TaskDataBuffer.<span style="color: #0000FF;">TaskAssignedToName_IsNull</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                TaskDataBuffer.<span style="color: #0000FF;">TaskId</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ID&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">TaskName</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;name&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">TaskPlannedCompletionDate</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;plannedCompletionDate&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">TaskPlannedStartDate</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;plannedStartDate&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
                TaskDataBuffer.<span style="color: #0000FF;">TaskProgressStatus</span> <span style="color: #008000;">=</span> 
                    item.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;progressStatus&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            page<span style="color: #008000;">++;</span>
        <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>items.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> rowsPerPage<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Remember that each API you interface with will have it's own nuances to deal with and the above code is pretty specific for the AtTask API I was working with.  The part I want to point out is after we do all the API busy work, we have that JSON feed in a stream, turn it in to XML, and then access the data using XPath selects.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2011/07/consuming-an-authenticated-json-feed-with-ssis/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Revisiting Embedded Text Qualifiers and the SSIS Flat File Connection Manager</title>
		<link>http://timlaqua.com/2011/06/revisiting-embedded-text-qualifiers-and-the-ssis-flat-file-connection-manager/</link>
		<comments>http://timlaqua.com/2011/06/revisiting-embedded-text-qualifiers-and-the-ssis-flat-file-connection-manager/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 01:45:42 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[ssis]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=607</guid>
		<description><![CDATA[To quickly summarize the problem at hand, the Flat File Connection Manager doesn't support embedded Text Qualifiers. The reason this is a big issue is because the most commonly used text qualifier is a double quote (&#34;) and that's also a commonly used punctuation mark. That means if you have a CSV to import and [...]]]></description>
			<content:encoded><![CDATA[<p>To quickly summarize the problem at hand, the Flat File Connection Manager doesn't support embedded Text Qualifiers.  The reason this is a big issue is because the most commonly used text qualifier is a double quote (&quot;) and that's also a commonly used punctuation mark.  That means if you have a CSV to import and lines that looks like this:<br />
<code><br />
"one","1","two","2","th""r""ee","3","asdf"<br />
"""left","right""","mid""dle","w"",tf","w,""tf",",""omg","omg"","<br />
</code></p>
<p>You would expect to import</p>
<table>
<tr>
<td>one</td>
<td>1</td>
<td>two</td>
<td>2</td>
<td>th"r"ee</td>
<td>3</td>
<td>asdf</td>
</tr>
<tr>
<td>"left</td>
<td>right"</td>
<td>mid"dle</td>
<td>w",tf</td>
<td>w,"tf</td>
<td>,"omg</td>
<td>omg",</td>
</tr>
</table>
<p>That's not what you get, you get an error.  I've seen a few different approaches to working around this known issue:</p>
<ul>
<li><a href="http://www.andrewdenhertog.com/tips/importing-csv-flat-files-in-ssis-dealing-with-double-quotes">Replacing embedded text qualifiers with some other token - &amp;quot; in this case</a></li>
<li><a href="http://www.ideaexcursion.com/2008/11/12/handling-embedded-text-qualifiers/">Import each file as a single column and parse the row in a script task, handling error rows</a></li>
</ul>
<p>What I'd like to explore in this post is using regular expressions to transform the entire file in to something that can be natively consumed by SSIS.  There are two options for dealing with this:</p>
<ul>
<li>Tokenizing the quotation marks found within the cells</li>
<li>Change the text qualifier</li>
</ul>
<p>I chose to change the text qualifier because SSIS natively supports multiple character text qualifiers.  This will allow me to pick something ridiculous that is incredibly unlikely to appear in the data naturally.  Changing the text qualifier also means we won't have to add any extra transformations as we would if we tokenized the embeded quotation marks (to turn them back in to quotation marks).<br />
<span id="more-607"></span><br />
Let's start out with the file we're importing so we have some text to test against (C:\SSISTextQualifier\Input.csv)</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;one&quot;,&quot;1&quot;,&quot;two&quot;,&quot;2&quot;,&quot;th&quot;&quot;r&quot;&quot;ee&quot;,&quot;3&quot;,&quot;asdf&quot;
&quot;&quot;&quot;left&quot;,&quot;right&quot;&quot;&quot;,&quot;mid&quot;&quot;dle&quot;,&quot;w&quot;&quot;,tf&quot;,&quot;w,&quot;&quot;tf&quot;,&quot;,&quot;&quot;omg&quot;,&quot;omg&quot;&quot;,&quot;</pre></div></div>

<p>Now let's iron out the regular expression we're going to need.</p>
<p>We'll start out with grabbing a bunch of stuff between two double-quotes<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/Regex07.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/Regex07.png" alt="" title="Regex07" width="620" height="343" class="alignnone size-full wp-image-624" /></a><br />
<br/><br />
Ok, we don't just want a bunch of stuff - we want the stuff between each set of quotation marks - we make the * quantifier lazy with a question mark<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/Regex01.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/Regex01.png" alt="" title="Regex01" width="620" height="315" class="alignnone size-full wp-image-616" /></a><br />
<br/><br />
Better... but that th""r""ee is clearly wrong... we have to stop the final quotation mark from matching properly escaped quotation marks.  We can do that with a negative lookahead:<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/Regex12.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/Regex12.png" alt="" title="Regex12" width="620" height="343" class="alignnone size-full wp-image-677" /></a><br />
<br/><br />
That's different - notice that now properly escaped quotation marks aren't abnormally terminating the column match anymore, now they're just acting like normal characters.  We have to make a special exception for escaped quotation marks.  Those have to essentially count as a single character:<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/Regex08.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/Regex08.png" alt="" title="Regex08" width="620" height="343" class="alignnone size-full wp-image-627" /></a><br />
<br/><br />
Perfect - Now we can replace the text qualifiers with something awesome like |~| since we have matched the column content safely<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/Regex05.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/Regex05.png" alt="" title="Regex05" width="620" height="315" class="alignnone size-full wp-image-621" /></a></p>
<p>Now that we have our regex, we just have to decide how to execute.</p>
<h3>Plan A:  SSIS Script Task</h3>
<p>Create a new package with a single script task in it<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/SSIS01.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/SSIS01.png" alt="" title="SSIS01" width="660" height="418" class="alignnone size-full wp-image-637" /></a></p>
<p>Now edit the script (C#) and add the following up by the existing using statements:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.RegularExpressions</span><span style="color: #008000;">;</span></pre></div></div>

<p>Modify the Main method to look something like this (depending on your paths):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            StreamReader sr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">StreamReader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;C:\SSISTextQualifier\Input.csv&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span> allText <span style="color: #008000;">=</span> sr.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sr.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            StreamWriter sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamWriter<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;C:\SSISTextQualifier\InputCleaned.csv&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sw.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>
                    Regex.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span>
                        allText, 
                        <span style="color: #666666;">@&quot;&quot;</span><span style="color: #666666;">&quot;((?:&quot;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #666666;">&quot;|.)*?)&quot;</span><span style="color: #666666;">&quot;(?!&quot;</span><span style="color: #666666;">&quot;)&quot;</span>, 
                        <span style="color: #666666;">@&quot;|~|$1|~|&quot;</span>
                    <span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span><span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sw.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Dts.<span style="color: #0000FF;">TaskResult</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>ScriptResults.<span style="color: #0000FF;">Success</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Then run that task and you will have a new file called C:\SSISTextQualifier\InputCleaned.csv that's text qualified by |~| and the escaped double quotes have been replaced with a single double quote.</p>
<h3>Plan B: Powershell</h3>
<p>Since we often call SSIS packages from the SQL Server Agent, we can easily add a Powershell step right before the SSIS package to copy and cleanse our file.  If that's the route you choose, your powershell step will look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">gc</span> C:\SSISTextQualifier\Input.csv <span style="color: pink;">|</span> 
<span style="color: pink;">%</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span> <span style="color: #FF0000;">-replace</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`&quot;</span>((?:<span style="color: #008080; font-weight: bold;">`&quot;</span><span style="color: #008080; font-weight: bold;">`&quot;</span>|.)*?)<span style="color: #008080; font-weight: bold;">`&quot;</span>(?!<span style="color: #008080; font-weight: bold;">`&quot;</span>)&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;|~|<span style="color: #008080; font-weight: bold;">`$</span>1|~|&quot;</span> <span style="color: #FF0000;">-replace</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`&quot;</span><span style="color: #008080; font-weight: bold;">`&quot;</span>&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`&quot;</span>&quot;</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> 
<span style="color: #008080; font-weight: bold;">sc</span> C:\SSISTextQualifier\InputCleaned2.csv</pre></div></div>

<h3>Final Steps</h3>
<p>However you got your data in to a new file (<strong>C:\SSISTextQualifier\InputCleaned.csv</strong> in my case) with a new text qualifier, you still have to then import the file.  This is done using our old friend the Flat File Connection Manager with the source being the newly minted clean file and with our new text qualifier set as |~|</p>
<p><a href="http://timlaqua.com/wp-content/uploads/2011/06/SSIS02.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/SSIS02.png" alt="" title="SSIS02" width="550" height="562" class="alignnone size-full wp-image-642" /></a></p>
<p>And that's pretty much it:<br/><br />
<a href="http://timlaqua.com/wp-content/uploads/2011/06/SSIS03.png"><img src="http://timlaqua.com/wp-content/uploads/2011/06/SSIS03.png" alt="" title="SSIS03" width="550" height="562" class="alignnone size-full wp-image-643" /></a></p>
<h3>Conclusion</h3>
<p>Personally, I think the Powershell plan is the cleanest and easiest to implement.  The code isn't hidden away in a script task, and it looks cool.  Doing it this way allows us to kind of abstract this "file fixing" from the SSIS data flows and let them do what they do best.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2011/06/revisiting-embedded-text-qualifiers-and-the-ssis-flat-file-connection-manager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallel Cube Processor 1.0</title>
		<link>http://timlaqua.com/2011/04/parallel-cube-processor-1-0/</link>
		<comments>http://timlaqua.com/2011/04/parallel-cube-processor-1-0/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 11:30:58 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[ssas]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=505</guid>
		<description><![CDATA[Parallel Cube Processor (PCP) is an application designed to process multiple Analysis Services databases at the same time. I originally wrote this application with the assumption that it would be blazing fast and essentially warp space and time. As it turns out, SSAS appears to be going as fast as possible at all times, so [...]]]></description>
			<content:encoded><![CDATA[<p>Parallel Cube Processor (PCP) is an application designed to process multiple Analysis Services databases at the same time.  I originally wrote this application with the assumption that it would be blazing fast and essentially warp space and time.  As it turns out, SSAS appears to be going as fast as possible at all times, so giving it more stuff to do doesn't really make the whole shebang faster - just different.  Play around with it, watch some performance counters - it's an interesting conundrum.  The application can handle parallelism using two methods.  </p>
<p>The first method is <strong>XMLAParallel</strong> - in this mode, the application constructs an XMLA command to process the specified number of databases inside of a Parallel element.  Only one XMLA command is issued at a time so the applicatoin will not submit another command until the longest processing job completes in each batch.  <span id="more-505"></span></p>
<p>The second method is <strong>Threaded</strong> - in this mode, the application creates the specified number of threads and each thread processes databases independantly of the other threads.  This allows all threads to remain busy at all times as a given thread will start processing the next unprocessed database as soon as it finishes processing the previous database.</p>
<p>It should be noted that because this app performs only ProcessFull at the database level, it is currently only suitable for processing a large number of small analysis services databases. The environment this was developed for has a process that generates and deploys metadata for over 250 small (< 100MB) ssas databases and we need to process them as quickly as possible.</p>
<p>If you have suggestions for features that would help the application be more useful to you, please post them in the Discussions area.</p>
<p>And yes, I probably should have called it "ParallelDatabaseProcessor." PCP was a cooler acronym.</p>
<p>The application and source code can be downloaded on Codeplex: <a href="http://pcp.codeplex.com/">Parallel Cube Processor</a></p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2011/04/parallel-cube-processor-1-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting a useful FTP error message out of SSIS</title>
		<link>http://timlaqua.com/2010/10/getting-a-useful-ftp-error-message-out-of-ssis/</link>
		<comments>http://timlaqua.com/2010/10/getting-a-useful-ftp-error-message-out-of-ssis/#comments</comments>
		<pubDate>Sat, 30 Oct 2010 14:10:55 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[ftp]]></category>
		<category><![CDATA[ssis]]></category>
		<category><![CDATA[ssis event handler]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=385</guid>
		<description><![CDATA[The plan: Upload a zip file for a bunch of different clients to various user-specified ftp servers - looks something like this: A quick note on what that "Update FTP Connection" task is doing - it's modifying the properties of the "FTP" connection manager to the appropriate Server/Username/Password for this particular client: 1 2 3 [...]]]></description>
			<content:encoded><![CDATA[<p>The plan:  Upload a zip file for a bunch of different clients to various user-specified ftp servers - looks something like this:<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ControlFlow.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ControlFlow.png" alt="" title="FTPErrors-ControlFlow" width="272" height="197" class="alignnone size-full wp-image-391" /></a></p>
<p><em>A quick note on what that "Update FTP Connection" task is doing - it's modifying the properties of the "FTP" connection manager to the appropriate Server/Username/Password for this particular client:</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            ConnectionManager ftp <span style="color: #008000;">=</span> Dts.<span style="color: #0000FF;">Connections</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;FTP&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            ftp.<span style="color: #0000FF;">Properties</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ServerName&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>ftp, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>Dts.<span style="color: #0000FF;">Variables</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;FTPServer&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ftp.<span style="color: #0000FF;">Properties</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ServerUserName&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>ftp, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>Dts.<span style="color: #0000FF;">Variables</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;FTPUser&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ftp.<span style="color: #0000FF;">Properties</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ServerPassword&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>ftp, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>Dts.<span style="color: #0000FF;">Variables</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;User::FTPPassword&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Dts.<span style="color: #0000FF;">TaskResult</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>ScriptResults.<span style="color: #0000FF;">Success</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The problem: Anyone who has tried to upload multiple files to multiple FTP sites in an SSIS package very quickly ran in to FTP errors (that's what happens when you let users tell you what their FTP url and authentication is).  You can handle these errors and log them via the standard OnError handler dumping the ErrorCode, ErrorDescription, and usually SourceName out to a flat file or table.<br />
<span id="more-385"></span></p>
<p>Now, when you look at the ErrorDescription for an FTP Task - you'll be disappointed.  It usually states "Unable to connect to FTP server..." or something to that effect.  Looking at the execution results in the progress tab, you'll see that the FTP Connection Manager threw a more detailed description of the error:<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ProgressScreen.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ProgressScreen-300x90.png" alt="" title="FTPErrors-ProgressScreen" width="300" height="90" class="alignnone size-medium wp-image-395" /></a></p>
<p>Good luck getting at that detailed error description - My first attempt here was to acquire a connection and connect via the connection manager and trap the error that came back.  While that worked, the error that was trapped was HRESULT 0xC001602A, which basically means that something bad happened.</p>
<p>Now we move on to phase two:  Retry the FTP operation via .NET FtpWebRequest and trap that error.  This can either flat out replace the FTP Task as a script task or, not sure why I did it this way - I suppose I like to try and use SSIS built in tasks as much as possible - you can retry the operation in the OnError handler assuming you have the requisite information (url, username, and password) available.</p>
<p>Create the OnError handler (either at the package level for everything or just on the executable you care about - I chose the latter):<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-CreateOnError.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-CreateOnError.png" alt="" title="FTPErrors-CreateOnError" width="212" height="94" class="alignnone size-full wp-image-392" /></a></p>
<p>Then add a data flow task - inside that add a Script Component (as a source) and an appropriate destination (Here it's going to an OleDb destination):<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-OnErrorDataFlowTasks.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-OnErrorDataFlowTasks.png" alt="" title="FTPErrors-OnErrorDataFlowTasks" width="175" height="164" class="alignnone size-full wp-image-394" /></a></p>
<p>Configure the Script Component for ReadOnly access to the requisite variables:<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ScriptComponentVars.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ScriptComponentVars-300x236.png" alt="" title="FTPErrors-ScriptComponentVars" width="300" height="236" class="alignnone size-medium wp-image-397" /></a></p>
<p>And configure the Output buffer:<br />
<a href="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ScriptComponentOutput.png"><img src="http://timlaqua.com/wp-content/uploads/2010/10/FTPErrors-ScriptComponentOutput-300x173.png" alt="" title="FTPErrors-ScriptComponentOutput" width="300" height="173" class="alignnone size-medium wp-image-396" /></a></p>
<p>Now go edit the actual script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Pipeline.Wrapper</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Runtime.Wrapper</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>Microsoft.<span style="color: #0000FF;">SqlServer</span>.<span style="color: #0000FF;">Dts</span>.<span style="color: #0000FF;">Pipeline</span>.<span style="color: #0000FF;">SSISScriptComponentEntryPointAttribute</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ScriptMain <span style="color: #008000;">:</span> UserComponent
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> CreateNewOutputRows<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">string</span> errorDescription <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">ErrorDescription</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Variables.<span style="color: #0000FF;">ErrorDescription</span>.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Unable to connect to FTP server&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                FtpWebRequest ftp <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>FtpWebRequest<span style="color: #000000;">&#41;</span>WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ftp://&quot;</span> <span style="color: #008000;">+</span> Variables.<span style="color: #0000FF;">FTPUrl</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                ftp.<span style="color: #0000FF;">Method</span> <span style="color: #008000;">=</span> WebRequestMethods.<span style="color: #0000FF;">Ftp</span>.<span style="color: #0000FF;">UploadFile</span><span style="color: #008000;">;</span>
                ftp.<span style="color: #0000FF;">Credentials</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Net</span></span>.<span style="color: #0000FF;">NetworkCredential</span><span style="color: #000000;">&#40;</span>Variables.<span style="color: #0000FF;">FTPUser</span>, Variables.<span style="color: #0000FF;">FTPPassword</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                StreamReader sourceStream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>Variables.<span style="color: #0000FF;">FTPLocalPath</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> fileContents <span style="color: #008000;">=</span> Encoding.<span style="color: #0000FF;">UTF8</span>.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>sourceStream.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                sourceStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                ftp.<span style="color: #0000FF;">ContentLength</span> <span style="color: #008000;">=</span> fileContents.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
&nbsp;
                Stream requestStream <span style="color: #008000;">=</span> ftp.<span style="color: #0000FF;">GetRequestStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                requestStream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>fileContents, <span style="color: #FF0000;">0</span>, fileContents.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                requestStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                FtpWebResponse response <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>FtpWebResponse<span style="color: #000000;">&#41;</span>ftp.<span style="color: #0000FF;">GetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                response.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                errorDescription <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Upload retry attempt via .NET was successful.&quot;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                errorDescription <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">Message</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
        ErrorSourceBuffer.<span style="color: #0000FF;">AddRow</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">ErrorCode</span> <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">ErrorCode</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">ErrorDescription</span> <span style="color: #008000;">=</span> errorDescription<span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">ErrorSource</span> <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">SourceName</span><span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">FTPUrl</span> <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">FTPUrl</span><span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">FTPUser</span> <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">FTPUser</span><span style="color: #008000;">;</span>
        ErrorSourceBuffer.<span style="color: #0000FF;">FTPPassword</span> <span style="color: #008000;">=</span> Variables.<span style="color: #0000FF;">FTPPassword</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And bob's your uncle.  Now you have meaningful text in the ErrorDescription column of the output buffer instead of some vague "something's wrong" message.</p>
<p>Here's a flat file destination walkthrough for the OnError handler: <a href="http://vsteamsystemcentral.com/cs21/blogs/applied_business_intelligence/archive/2007/04/08/ssis-design-pattern-custom-error-handling.aspx">http://vsteamsystemcentral.com/cs21/blogs/applied_business_intelligence/archive/2007/04/08/ssis-design-pattern-custom-error-handling.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/10/getting-a-useful-ftp-error-message-out-of-ssis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding Windows Event Log Logging To Existing SSIS Packages via C#</title>
		<link>http://timlaqua.com/2009/03/adding-windows-event-log-logging-to-existing-ssis-packages-via-c/</link>
		<comments>http://timlaqua.com/2009/03/adding-windows-event-log-logging-to-existing-ssis-packages-via-c/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 02:19:45 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[integration services]]></category>
		<category><![CDATA[ssis]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=182</guid>
		<description><![CDATA[While working with some SSIS logging, it occured to me that there's really no good way to globally apply the same logging to all packages on your server. For our production SSIS server, it would make sense to log all OnError events to the Application Event log. Then, whatever event log monitoring app you use [...]]]></description>
			<content:encoded><![CDATA[<p>While working with some SSIS logging, it occured to me that there's really no good way to globally apply the same logging to all packages on your server.  For our production SSIS server, it would make sense to log all OnError events to the Application Event log.  Then, whatever event log monitoring app you use can notify you of the package error (with details!).  Thus, this app was born - an app to remove pre-existing LogProviders and add in the intended LogProvider and options.</p>
<p>This is really just an example of the implementation that worked for my purposes - I'm sure some of you would prefer to log elsewhere.  This should be enough to get you going.  Please drop a line in the Comments if you post alternative approaches.</p>
<p><u>Usage</u></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">SSISForcedLogging.Console.exe &quot;Z:\SSIS Packages&quot;
or
SSISForcedLogging.Console.Exe &quot;Z:\SSIS Packages\Package1.dtsx&quot;</pre></div></div>

<p><span id="more-182"></span><br />
For the following code, be sure to add References to:</p>
<ul>
<li>Microsoft.SqlServer.ManagedDTS</li>
<li>System.configuration</li>
</ul>
<p><u>Program.cs</u></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Configuration</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Dts.Runtime</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> SSISForcedLogging.<span style="color: #0000FF;">Console</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">class</span> ColumnFilter
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Computer<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> DataBytes<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> ExecutionID<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> MessageText<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Operator<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> SourceID<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> SourceName<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> DTSEventColumnFilter GetDTSEventColumnFilter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            DTSEventColumnFilter dtsEventColumnFilter <span style="color: #008000;">=</span> 
                <span style="color: #008000;">new</span> DTSEventColumnFilter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">DataBytes</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">DataBytes</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">Computer</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Computer</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">ExecutionID</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ExecutionID</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">MessageText</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">MessageText</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0600FF;">Operator</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Operator</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">SourceID</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SourceID</span><span style="color: #008000;">;</span>
            dtsEventColumnFilter.<span style="color: #0000FF;">SourceName</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SourceName</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> dtsEventColumnFilter<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #FF0000;">class</span> Program
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>args.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Console</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
                    <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>Please specify a package path or directory<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Environment.<span style="color: #0000FF;">Exit</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #FF0000;">string</span> arg <span style="color: #008000;">=</span> args<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> packages <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>File.<span style="color: #0000FF;">Exists</span><span style="color: #000000;">&#40;</span>arg<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                packages.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>arg<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Directory.<span style="color: #0000FF;">Exists</span><span style="color: #000000;">&#40;</span>arg<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                packages <span style="color: #008000;">=</span> GetPackagesFromDirectory<span style="color: #000000;">&#40;</span>arg<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Console</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
                    <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>Invalid package path or directory specified<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Environment.<span style="color: #0000FF;">Exit</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> pkgPath <span style="color: #0600FF;">in</span> packages<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Console</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
                    <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Updating: {0}&quot;</span>, pkgPath<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Application dts <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Application<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Package pkg <span style="color: #008000;">=</span> dts.<span style="color: #0000FF;">LoadPackage</span><span style="color: #000000;">&#40;</span>pkgPath, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                RemoveOldLogsFromPackage<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> pkg<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                AddLogToPackage<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> pkg<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Console</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>
                    <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Saving: {0}&quot;</span>, pkgPath<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                dts.<span style="color: #0000FF;">SaveToXml</span><span style="color: #000000;">&#40;</span>pkgPath, pkg, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> AddLogToPackage<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> Package Pkg<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// Add new provider</span>
            Pkg.<span style="color: #0000FF;">LoggingMode</span> <span style="color: #008000;">=</span> DTSLoggingMode.<span style="color: #0000FF;">Enabled</span><span style="color: #008000;">;</span>
&nbsp;
            LogProvider log <span style="color: #008000;">=</span> Pkg.<span style="color: #0000FF;">LogProviders</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>
                ConfigurationManager.<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;logType&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            log.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> 
                ConfigurationManager.<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;logName&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            log.<span style="color: #0000FF;">Description</span> <span style="color: #008000;">=</span> 
                ConfigurationManager.<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;logDesc&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            Pkg.<span style="color: #0000FF;">LoggingOptions</span>.<span style="color: #0000FF;">SelectedLogProviders</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>log<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            LoggingOptions logOptions <span style="color: #008000;">=</span> Pkg.<span style="color: #0000FF;">LoggingOptions</span><span style="color: #008000;">;</span>
            logOptions.<span style="color: #0000FF;">EventFilterKind</span> <span style="color: #008000;">=</span> DTSEventFilterKind.<span style="color: #0000FF;">Inclusion</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> events <span style="color: #008000;">=</span> 
                ConfigurationManager
                    .<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;eventsToLog&quot;</span><span style="color: #000000;">&#93;</span>
                        .<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">','</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            logOptions.<span style="color: #0000FF;">EventFilter</span> <span style="color: #008000;">=</span> events<span style="color: #008000;">;</span>
&nbsp;
            ArrayList columnList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ArrayList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            columnList.<span style="color: #0000FF;">AddRange</span><span style="color: #000000;">&#40;</span>
                ConfigurationManager
                    .<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;columnsToLog&quot;</span><span style="color: #000000;">&#93;</span>
                        .<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">','</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            ColumnFilter eventColumnFilter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ColumnFilter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>FieldInfo field <span style="color: #0600FF;">in</span> 
                     eventColumnFilter.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetFields</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>columnList.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>field.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    field.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>eventColumnFilter, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            logOptions.<span style="color: #0000FF;">SetColumnFilter</span><span style="color: #000000;">&#40;</span>
                <span style="color: #666666;">&quot;OnError&quot;</span>, 
                eventColumnFilter.<span style="color: #0000FF;">GetDTSEventColumnFilter</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> RemoveOldLogsFromPackage<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> Package Pkg<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// Remove existing log provider selections</span>
            <span style="color: #FF0000;">int</span> count <span style="color: #008000;">=</span> Pkg.<span style="color: #0000FF;">LoggingOptions</span>.<span style="color: #0000FF;">SelectedLogProviders</span>.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> count<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                Pkg.<span style="color: #0000FF;">LoggingOptions</span>.<span style="color: #0000FF;">SelectedLogProviders</span>.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Remove existing log providers</span>
            count <span style="color: #008000;">=</span> Pkg.<span style="color: #0000FF;">LogProviders</span>.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> count<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                Pkg.<span style="color: #0000FF;">LogProviders</span>.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            RemoveOldLogsFromExecutable<span style="color: #000000;">&#40;</span>Pkg.<span style="color: #0000FF;">Executables</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> RemoveOldLogsFromExecutable<span style="color: #000000;">&#40;</span>Executables Execs<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// reflection ftw</span>
            <span style="color: #FF0000;">int</span> count<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>Executable exec <span style="color: #0600FF;">in</span> Execs<span style="color: #000000;">&#41;</span> 
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>PropertyInfo prop <span style="color: #0600FF;">in</span> 
                         exec.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetProperties</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">switch</span><span style="color: #000000;">&#40;</span>prop.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;LoggingMode&quot;</span><span style="color: #008000;">:</span>
                            prop.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>
                                exec, 
                                DTSLoggingMode.<span style="color: #0000FF;">UseParentSetting</span>, 
                                <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                            break<span style="color: #008000;">;</span>
                        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;LoggingOptions&quot;</span><span style="color: #008000;">:</span>
                            LoggingOptions opts <span style="color: #008000;">=</span> 
                                prop.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>
                                    exec, 
                                    <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> LoggingOptions<span style="color: #008000;">;</span>
&nbsp;
                            count <span style="color: #008000;">=</span> opts.<span style="color: #0000FF;">SelectedLogProviders</span>.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
                            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> count<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                                opts.<span style="color: #0000FF;">SelectedLogProviders</span>.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                            break<span style="color: #008000;">;</span>
                        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;Executables&quot;</span><span style="color: #008000;">:</span>
                            Executables execs <span style="color: #008000;">=</span> 
                                prop.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>
                                exec, 
                                <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> Executables<span style="color: #008000;">;</span>
                            RemoveOldLogsFromExecutable<span style="color: #000000;">&#40;</span>execs<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                            break<span style="color: #008000;">;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> GetPackagesFromDirectory<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> Path<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> files <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> d <span style="color: #0600FF;">in</span> Directory.<span style="color: #0000FF;">GetDirectories</span><span style="color: #000000;">&#40;</span>Path<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> f <span style="color: #0600FF;">in</span> GetPackagesFromDirectory<span style="color: #000000;">&#40;</span>d<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    files.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>f<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> f <span style="color: #0600FF;">in</span> Directory.<span style="color: #0000FF;">GetFiles</span><span style="color: #000000;">&#40;</span>Path, <span style="color: #666666;">&quot;*.dtsx&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                files.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>f<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> files<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><u>App.config</u></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appSettings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;logType&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;DTS.LogProviderEventLog.1&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;logName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Windows Event Log Logging Provider (Forced)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;logDesc&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Logs Event info to Windows Event Log (Forced)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;eventsToLog&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;OnError&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;columnsToLog&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Computer,Operator,SourceName,SourceID,ExecutionID,MessageText&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appSettings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/03/adding-windows-event-log-logging-to-existing-ssis-packages-via-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

