<?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>.Net Smoothie &#187; data</title>
	<atom:link href="http://www.richardbushnell.net/tag/data/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardbushnell.net</link>
	<description></description>
	<lastBuildDate>Thu, 12 Mar 2009 13:52:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Easy Data-loading with LINQ-to-SQL and LINQ-to-XML</title>
		<link>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/</link>
		<comments>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 07:53:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[LINQ-to-XML]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[VB.Net]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[VB]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/</guid>
		<description><![CDATA[.Net 3.5 had some nice tricks in it. LINQ-to-XML was one of them. With the new &#34;X&#34;-types, you can make working with XML really easy. VB.Net 9 takes it one step further, and lets you write XML in your code without strings. &#34;Hey Rich, that&#8217;s old news,&#34; I hear you say. &#34;And who&#8217;s interested in [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F20%2Feasy-data-loading-with-linq-to-sql-and-linq-to-xml-4%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>.Net 3.5 had some nice tricks in it. LINQ-to-XML was one of them. With the new &quot;X&quot;-types, you can make working with XML really easy. </p>
<p>VB.Net 9 takes it one step further, and lets you write XML in your code without strings.</p>
<p>&quot;Hey Rich, that&#8217;s old news,&quot; I hear you say. &quot;And who&#8217;s interested in VB today anyway?&quot;</p>
<p>Well, apparently there are a lot of VB-er&#8217;s still out there. I am mainly a C# developer myself, but I found that VB was perfect for a problem I had recently -&#160; loading of XML data into a SQL Server table.</p>
<p><span id="more-98"></span></p>
<h2>SQL Server 2005 and XML Fields</h2>
<p>I recently came across the need to load a database with XML data fields in SQL Server 2005. I had a series of XML documents which all needed to be pumped into a table containing a field of type XML. I could have opened up SQL Server Management Studio and fired in a load of strings, but I was a bit concerned that the XML might not be formatted quite correctly, which would have caused problems in the loading process.</p>
<p>Another problem was that I wanted to be able to add dynamic XML. I wanted to generate some XML data on the fly. That&#8217;s not so easy with insert statements in SQL.</p>
<h2>LINQ-to-SQL and XML Fields</h2>
<p>Using my recently-acquired LINQ-to-SQL knowledge, I knew that the Entities generated for an XML field are translated to <strong>XElements</strong> &#8211; the new XML type in .Net 3.5. I also knew that VB 9 lets you create an <strong>XElement</strong> by using XML in code itself: </p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image15.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="200" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb11.png" width="445" border="0" /></a>&#160;</p>
<p>So I came up with a plan&#8230;</p>
<h2>Inserts with LINQ-to-SQL</h2>
<p>For data-loading, LINQ-to-SQL is magic. A simple Insert is a piece of cake.</p>
<p>First, create your <strong>DataContext</strong> using the Visual Studio template, then drag the table you are inserting to onto the <strong>DataContext</strong> canvas. That gives you an Entity class. You then can create an Entity in your code, call <strong>InsertOnSubmit</strong> on the <strong>DataContext</strong> table, then call <strong>SubmitChanges</strong> on the <strong>DataContext</strong> itself.</p>
<p>Using Object Property Initializers, you can make this really easy.</p>
<p>Here&#8217;s what it looks like with VB.Net using an XML datatype:</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image13.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="631" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb9.png" width="475" border="0" /></a> </p>
<p>(Sorry to have torn the edge off the code there, but it wouldn&#8217;t fit nicely in my post at full width. XML can be a <em>little </em>long sometimes.)</p>
<p>The great thing here is that any malformed XML is highlighted (or even automatically corrected) for me by the VB compiler. I can also add parameters and generate lots of different XML using the familiar ASP syntax:</p>
<div style="width:480px;margin-right:100px">
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image14.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="112" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb10.png" width="374" border="0" /></a>&#160;</p>
</div>
<div style="clear:left">
<p>And as <strong>SubmitChanges</strong> uses a single transaction, the data-loading can easily be an all-or-nothing affair. In addition, any foreign key relationships can easily be taken care of by assigning objects to each other using standard object notation. The <strong>DataContext</strong> takes care of doing things in the correct order for you.</p>
</div>
<p>It&#8217;s all super-easy, and takes about 1 minute to get started.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F20%2Feasy-data-loading-with-linq-to-sql-and-linq-to-xml-4%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
