<?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>BI Monkey &#187; ADO .NET</title>
	<atom:link href="http://www.bimonkey.com/tag/ado-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bimonkey.com</link>
	<description>James Beresford on Microsoft BI and Consulting in Sydney, Australia</description>
	<lastBuildDate>Mon, 23 Jan 2012 22:01:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>The ADO .NET Source and SQL in the Script Task</title>
		<link>http://www.bimonkey.com/2009/05/the-ado-net-source-and-sql-in-the-script-task/</link>
		<comments>http://www.bimonkey.com/2009/05/the-ado-net-source-and-sql-in-the-script-task/#comments</comments>
		<pubDate>Tue, 05 May 2009 08:10:21 +0000</pubDate>
		<dc:creator>BI Monkey</dc:creator>
				<category><![CDATA[Integration Services]]></category>
		<category><![CDATA[ADO .NET]]></category>
		<category><![CDATA[DataReader]]></category>

		<guid isPermaLink="false">http://www.bimonkey.com/?p=127</guid>
		<description><![CDATA[NB: In SQL 2005 the ADO .NET Source is called the DataReader Source, and can only be edited through the Advanced Editor. In terms of configuration options there is little difference between the ADO .NET source and the OLE DB source, with the exception that you cannot populate the ADO .NET source table/view name or [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;"><strong>NB:</strong></span> In SQL 2005 the ADO .NET Source is called the <strong>DataReader </strong>Source, and can only be edited through the Advanced Editor.</p>
<p>In terms of configuration options there is little difference between the ADO .NET source and the OLE DB source, with the exception that you cannot populate the ADO .NET source table/view name or SQL command from a variable. If you are looking for a guide to basic use of the component, <a title="The OLE DB Source component - The Basics" href="http://www.bimonkey.com/2009/04/the-ole-db-source-component-the-basics/">please read this post on the OLE DB Source</a> as the same rules apply.</p>
<p>The sample package can be found <a title="ADO .NET Source sample package (Right click, save as)" href="http://www.bimonkey.com/uploads/componentreview/ADO%20NET%20Source%20Component%20Basics.dtsx">here for 2008</a> and guidelines on use are <a title="Using samples from BI Monkey" href="http://www.bimonkey.com/support/using-ssis-samples-from-this-site/">here</a>.</p>
<p>The OLE DB source and ADO .NET source are so similar in fact, it is forgivable to ask why they are even separate components. The simple(ish) reason for this is that the two components talk to their underlying data sources in very different ways &#8211; the OLE DB source will talk directly to relational OLE DB compliant sources, but the ADO .NET source goes through a layer of abstraction so it can talk to the source through a .NET provider. If all that sounds like gibberish, don&#8217;t worry. You only need to consider using the ADO .NET source in two cases. Firstly if no OLE DB provider is available &#8211; e.g. if you need to talk to a SAP BI instance. Secondly if you need to access the source through a Script Task.</p>
<p><strong>Accessing a Connection Manager through the Script Task</strong></p>
<p><strong></strong>To access a relational source in a script task, you need to use an ADO .NET Connection Manager. Trying to use an OLE DB Connection will return an error, so if you need to read data from a SQL Server source into a script, you need to use the slower ADO .NET component.</p>
<p>The code itself is not overly complex, but if you are a non-coder like me, it&#8217;s pretty impenetrable. I&#8217;ve used Visual Basic, which I am assured by programmers is a clear sign I don&#8217;t know what I&#8217;m doing &#8211; so one day I will endeavour to pick up C#. Note that this is to access in a Script Task in the data flow &#8211; the code required for a Script Component is slightly different.</p>
<p>The code sample is below, slightly edited for clarity. It is lifted from the sample package for this post and simply counts the rows in a table and presents the results in a message box.</p>
<pre><span style="color: #000080;">Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Imports System.Data.SqlClient</span>

<span style="color: #000080;"> Dim sqlConn As SqlClient.SqlConnection</span>       <span style="color: #008000;">' Object to hold connection</span>
<span style="color: #000080;"> Dim sqlCmd As SqlCommand </span>                    <span style="color: #008000;">' SQL Command object</span>
<span style="color: #000080;"> Dim rdrReader As SqlDataReader  </span>             <span style="color: #008000;">' Data reader to hold output of command</span>
<span style="color: #000080;"> Dim sqlCmdText As String </span>                    <span style="color: #008000;">' SQL Command text</span>
<strong>
<span style="color: #000080;">Public Sub Main()</span></strong>

 <span style="color: #008000;">' Opening the connection manager for use - for more detail see MSDN at <a title="Connecting to Data Sources in the Script Task" href="http://msdn.microsoft.com/en-us/library/ms136018.aspx">http://msdn.microsoft.com/en-us/library/ms136018.aspx</a></span>
<span style="color: #000080;"> sqlConn = DirectCast(Dts.Connections("ADO .NET AdventureWorks2008").AcquireConnection(Dts.Transaction), SqlClient.SqlConnection)
 </span>
<span style="color: #008000;"> ' Set the command text</span>
<span style="color: #000080;"> sqlCmdText = "SELECT COUNT(*) FROM [Person].[Address]"</span>

<span style="color: #008000;"> ' Execute the command and put the results into the data reader</span>
<span style="color: #000080;"> sqlCmd = New SqlCommand(sqlCmdText, sqlConn)
 rdrReader = sqlCmd.ExecuteReader()</span>

<span style="color: #008000;"> ' Take the data item in the reader and show it in a message box</span>
<span style="color: #000080;"> rdrReader.Read()
 MsgBox(rdrReader.Item(0), , "The [Person].[Address] table contains this many rows:")
 rdrReader.Close()

 Dts.TaskResult = ScriptResults.Success</span></pre>
<pre><strong><span style="color: #000080;">End Sub</span></strong></pre>
<p>Obviously you can do much cleverer things with the data once you have it on board, but this helps you overcome the initial hurdle of getting access to it.</p>
<p><strong>ADO .NET vs OLE DB Connection Performance</strong></p>
<p>To demonstrate the effect of pushing the data through the extra processing that .NET providers, I&#8217;ve also included in the sample package two data Flows that simply shift the contents of a large table to a Raw File using an OLE DB Source and a ADO .NET source. On my PC the OLE DB task took 2.3 seconds, but the ADO .NET version took 4.2. So the lesson here is clearly that if you need to access OLE DB sources and don&#8217;t need the added functionality provided by the ADO .NET provider, don&#8217;t use it.</p>
<p><strong>Summary</strong></p>
<p>The ADO .NET Source is very similar to the OLE DB source, but adds overhead when extracting data from OLE DB compliant sources so should only be used to access those sources when specifically required, e.g. when they need to be access in code. For non OLE DB compliant sources, such as ODBC, it adds a wide range of connection capabilities and extends the number of sources SSIS can work against.</p>
<p>MDSN documentation for this component can be found here for <a title="SQL 2008 MSDN Documentation for the ADO .NET Source" href="http://msdn.microsoft.com/en-us/library/ms137897.aspx">2008</a> and here for <a title="SQL 2005 MSDN Documentation for the DataReader Source" href="http://msdn.microsoft.com/en-us/library/ms137897(SQL.90).aspx">2005</a> (as DataReader Source)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bimonkey.com/2009/05/the-ado-net-source-and-sql-in-the-script-task/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

