<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Arcane Code &#187; FILESTREAM</title>
	<atom:link href="http://arcanecode.com/category/filestream/feed/" rel="self" type="application/rss+xml" />
	<link>http://arcanecode.com</link>
	<description>Making Microsoft .Net Development Magical</description>
	<lastBuildDate>Tue, 07 Feb 2012 16:57:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='arcanecode.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Arcane Code &#187; FILESTREAM</title>
		<link>http://arcanecode.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://arcanecode.com/osd.xml" title="Arcane Code" />
	<atom:link rel='hub' href='http://arcanecode.com/?pushpress=hub'/>
		<item>
		<title>Full Text Searching a FILESTREAM VARBINARY (MAX) Column</title>
		<link>http://arcanecode.com/2009/05/28/full-text-searching-a-filestream-varbinary-max-column/</link>
		<comments>http://arcanecode.com/2009/05/28/full-text-searching-a-filestream-varbinary-max-column/#comments</comments>
		<pubDate>Thu, 28 May 2009 06:04:00 +0000</pubDate>
		<dc:creator>arcanecode</dc:creator>
				<category><![CDATA[FILESTREAM]]></category>
		<category><![CDATA[FTS]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server Full Text Searching]]></category>
		<category><![CDATA[Full Text Search]]></category>

		<guid isPermaLink="false">http://arcanecode.wordpress.com/2009/05/28/full-text-searching-a-filestream-varbinary-max-column/</guid>
		<description><![CDATA[In the past I’ve written that Full Text Searching has the ability to index documents stored in a VARBINARY(MAX) field. However, I have never really gone into any details on how to do this. Today I will remedy that by demonstrating how to Full Text Seach not only using a VARBINARY(MAX) field, but one that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1589&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the past I’ve written that Full Text Searching has the ability to index documents stored in a VARBINARY(MAX) field. However, I have never really gone into any details on how to do this. Today I will remedy that by demonstrating how to Full Text Seach not only using a VARBINARY(MAX) field, but one that has been stored using FILESTREAM. Even though these examples will be done against the data we’ve stored with FILESTREAM over the lessons from <a href="http://arcanecode.com/2009/05/18/enabling-filestream-on-sql-server-2008/">the last few days</a>, know that this technique is identical for binary objects stored in a VARBINARY(MAX) field without using FILESTREAM. </p>
<p>Let’s start by creating a catalog to hold our Full Text data. </p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=CREATE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">CREATE</a> FULLTEXT <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=CATALOG&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">CATALOG</a> FileStreamFTSCatalog <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=AS&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">AS</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=DEFAULT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">DEFAULT</a>;</pre>
<p>Pretty normal, now we need to create a full text index on the “DocumentRepository” table we created in <a href="http://arcanecode.com/2009/05/20/creating-tables-and-inserting-rows-with-filestream/">this series</a>. When you look at the syntax though, you may notice a minor difference from the CREATE FULLTEXT INDEX examples I’ve shown in the past:</p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=CREATE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">CREATE</a> FULLTEXT <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INDEX&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INDEX</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=ON&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">ON</a> dbo.DocumentRepository
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">(DocumentName, Document TYPE <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=COLUMN&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">COLUMN</a> DocumentExtension)
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=KEY&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">KEY</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INDEX&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INDEX</a> PK__Document__3214EC277F60ED59
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=ON&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">ON</a> FileStreamFTSCatalog
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=WITH&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">WITH</a> CHANGE_TRACKING AUTO;
</pre>
<p>Here you can see I am indexing two fields. The first is the “DocumentName”, which is passed in as the first parameter and looks like other examples. We won’t actually be using it in this example, however I included it to demonstrate you can index multiple columns even when one of them is a VARBINARY(MAX) column. </p>
<p>The second parameter indexes the VARBINARY(MAX) “Document” column itself, but notice the TYPE COLUMN after the column name. In order to Full Text Index a VARBINARY(MAX) column you must also have a column with the file extension in it. You then pass in the name of column after the TYPE COLUMN. In this example, the document extension is stored in the “DocumentExtension” column. Since the document extension can be stored in a column with any name, we let the Full Text engine know which column by passing it in after the TYPE COLUMN keyword. The remainder of the command is like other examples <a href="http://arcanecode.com/2007/06/27/getting-started-with-sql-server-2005-full-text-searching-part-2-%E2%80%93-the-indexes/">I’ve shown in the past</a>. </p>
<p>Now we can run a normal SELECT…CONTAINS query against the “Document” field. </p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=SELECT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">SELECT</a> ID, DocumentName
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=FROM&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">FROM</a> dbo.DocumentRepository
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=WHERE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">WHERE</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=CONTAINS&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">CONTAINS</a>(Document, '<span style="color:#8b0000;">Shrew</span>');
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"></pre>
<p>I’ll leave it to you to run, for me it returned one row, with “TheTamingOfTheShrew.doc”. If you want to try it again, use “Elinor”, and you should get back “KingJohn.doc”. </p>
<p>As you can see, performing a Full Text Search against a VARBINARY(MAX) column is quite easy, all you have to do is indicate the document type by using the TYPE COLUMN. There are two more things you should know. First, the column containing the document extension must be of type CHAR, NCHAR, VARCHAR, or NVARCHAR. Second, the document type must be recognized by SQL Server. To get a list of all valid document types, simply query the fulltext_document_types catalog view like so: </p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=SELECT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">SELECT</a> * <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=FROM&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">FROM</a> sys.fulltext_document_types;</pre>
<p>This will give you a list of all file extensions understood by SQL Server. Each row actually represents a filter. Each filter represents a DLL that implements the IFilter interface. It is possible to add additional filters to the system. For example, Microsoft offers the “Microsoft Filter Pack”. You may have noticed that out of the box SQL Server 2008 supports the older Office 2003 documents, but not the more recent Office 2007 formats. To add these newer formats to your SQL Server, Microsoft provides the afore mentioned filter pack. While installing it is beyond the scope of this aritcle you can find complete instructions for downloand and installation at <a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;945934">http://support.microsoft.com/default.aspx?scid=kb;en-us;945934</a> . </p>
<p>The Full Text Search features provided by SQL Server continue to amaze me with how powerful they are, yet how easy they are to implment. With the information here you can easily search through documents stored in a VARBINARY(MAX) field, even when those documents are actually stored via the new SQL Server 2008 FILESTREAM. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/arcanecode.wordpress.com/1589/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/arcanecode.wordpress.com/1589/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/arcanecode.wordpress.com/1589/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/arcanecode.wordpress.com/1589/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/arcanecode.wordpress.com/1589/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/arcanecode.wordpress.com/1589/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/arcanecode.wordpress.com/1589/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/arcanecode.wordpress.com/1589/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/arcanecode.wordpress.com/1589/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/arcanecode.wordpress.com/1589/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/arcanecode.wordpress.com/1589/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/arcanecode.wordpress.com/1589/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/arcanecode.wordpress.com/1589/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/arcanecode.wordpress.com/1589/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1589&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://arcanecode.com/2009/05/28/full-text-searching-a-filestream-varbinary-max-column/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8436cd3ff437697b2608405422efa4da?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">arcanecode</media:title>
		</media:content>
	</item>
		<item>
		<title>Accessing FILESTREAM Data From A Client .NET Application &#8211; Part 2 Downloading a File</title>
		<link>http://arcanecode.com/2009/05/27/accessing-filestream-data-from-a-client-net-application-part-2-downloading-a-file/</link>
		<comments>http://arcanecode.com/2009/05/27/accessing-filestream-data-from-a-client-net-application-part-2-downloading-a-file/#comments</comments>
		<pubDate>Wed, 27 May 2009 06:03:00 +0000</pubDate>
		<dc:creator>arcanecode</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[C Sharp]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[FILESTREAM]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://arcanecode.wordpress.com/2009/05/27/accessing-filestream-data-from-a-client-net-application-part-2-downloading-a-file/</guid>
		<description><![CDATA[In the previous entry we covered how to upload a file to SQL Server using the FILESTREAM, new to SQL Server 2008. In this post we will look at retrieving a file from SQL Server using FILESTREAM. If you missed yesterday’s installment, a simple front end was created, the full project can be found at [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1584&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the previous entry we covered how to upload a file to SQL Server using the FILESTREAM, new to SQL Server 2008. In this post we will look at retrieving a file from SQL Server using FILESTREAM. If you missed yesterday’s installment, a simple front end was created, the full project can be found at the Code Gallery site <a href="http://code.msdn.microsoft.com/FileStreamFTS">http://code.msdn.microsoft.com/FileStreamFTS</a> . </p>
<p>The interface is very simple:</p>
<p><a href="http://arcanecode.files.wordpress.com/2009/05/image4.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://arcanecode.files.wordpress.com/2009/05/image_thumb4.png?w=495&#038;h=347" width="495" height="347" /></a> </p>
<p>The grid is a Data View Grid that shows the ID and Document information from the table we previously created. (If you want to see the code to populate the grid see the project.) The user picks a row, then clicks on the Get File button. </p>
<div style="font-family:consolas;background:white;color:black;font-size:10pt;">
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">private</span> <span style="color:blue;">void</span> btnGetFile_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;">&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Reset in case it was used previously</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; lblStatus.Text = <span style="color:#a31515;">&quot;&quot;</span>;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span> (dgvFiles.CurrentRow != <span style="color:blue;">null</span>)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Grab the ID (Primary Key) for the current row</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">int</span> ID = (<span style="color:blue;">int</span>)dgvFiles.CurrentRow.Cells[0].Value;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Now Save the file to the folder passed in the second</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// paramter. </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">FileTransfer</span>.GetFile2(ID, <span style="color:#a31515;">@&quot;D:\Docs\Output\&quot;</span>);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// And let user know it&#8217;s OK </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; lblStatus.Text = <span style="color:#a31515;">&quot;File Retrieved&quot;</span>;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160; }</p>
</p></div>
<p>The code is very simple, the heart of it is the FileTransfer.GetFile static method. Two values are passed in, the integer ID, which is the primary key from the database, and the path to save the file to. Here I simply hard coded a path, in a real life application you will want to give the user the ability to enter a path. Let’s take a look at the GetFile routine.</p>
<div style="font-family:consolas;background:white;color:black;font-size:10pt;">
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> GetFile(<span style="color:blue;">int</span> ID, <span style="color:blue;">string</span> outputPath)</p>
<p style="margin:0;">&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Setup database connection</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">SqlConnection</span> sqlConnection = <span style="color:blue;">new</span> <span style="color:#2b91af;">SqlConnection</span>(</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#a31515;">&quot;Integrated Security=true;server=(local)&quot;</span>);</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">SqlCommand</span> sqlCommand = <span style="color:blue;">new</span> <span style="color:#2b91af;">SqlCommand</span>();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; sqlCommand.Connection = sqlConnection;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">try</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; sqlConnection.Open();</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Everything we do with FILESTREAM must always be in </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// the context of a transaction, so we&#8217;ll start with </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// creating one.</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">SqlTransaction</span> transaction </p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; = sqlConnection.BeginTransaction(<span style="color:#a31515;">&quot;mainTranaction&quot;</span>);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; sqlCommand.Transaction = transaction;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// The SQL gives us 3 values. First the PathName() method of </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// the Document field is called, we&#8217;ll need it to use the API</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Second we call a special function that will tell us what</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// the context is for the current transaction, in this case </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// the &quot;mainTransaction&quot; we started above. Finally it gives</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// the name of the document, which the app will use when it</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// creates the document but is not strictly required as </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// part of the FILESTREAM.</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; sqlCommand.CommandText</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; = <span style="color:#a31515;">&quot;SELECT Document.PathName()&quot;</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="color:#a31515;">&quot;, GET_FILESTREAM_TRANSACTION_CONTEXT() &quot;</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="color:#a31515;">&quot;, DocumentName &quot;</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="color:#a31515;">&quot;FROM FileStreamFTS.dbo.DocumentRepository &quot;</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="color:#a31515;">&quot;WHERE ID=@theID &quot;</span>;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; sqlCommand.Parameters.Add(</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#a31515;">&quot;@theID&quot;</span>, <span style="color:#2b91af;">SqlDbType</span>.Int).Value = ID;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">SqlDataReader</span> reader = sqlCommand.ExecuteReader();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span> (reader.Read() == <span style="color:blue;">false</span>)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">throw</span> <span style="color:blue;">new</span> <span style="color:#2b91af;">Exception</span>(<span style="color:#a31515;">&quot;Unable to get BLOB data&quot;</span>);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// OK we have some data, pull it out of the reader into locals</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">string</span> path = (<span style="color:blue;">string</span>)reader[0];</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">byte</span>[] context = (<span style="color:blue;">byte</span>[])reader[1];</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">string</span> outputFilename = (<span style="color:blue;">string</span>)reader[2];</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">int</span> length = context.Length;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; reader.Close();</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Now we need to use the API we declared at the top of this class</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// in order to get a handle. </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">SafeFileHandle</span> handle = OpenSqlFilestream(</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; path</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , DESIRED_ACCESS_READ</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , SQL_FILESTREAM_OPEN_NO_FLAGS</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , context</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , (<span style="color:#2b91af;">UInt32</span>)length, 0);</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Using the handle we just got, we can open up a stream from </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// the database.</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">FileStream</span> databaseStream = <span style="color:blue;">new</span> <span style="color:#2b91af;">FileStream</span>(</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; handle, <span style="color:#2b91af;">FileAccess</span>.Read);</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// This file stream will be used to copy the data to disk</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">FileStream</span> outputStream </p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; = <span style="color:#2b91af;">File</span>.Create(outputPath + outputFilename); </p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Setup a buffer to hold the streamed data</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">int</span> blockSize = 1024 * 512;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">byte</span>[] buffer = <span style="color:blue;">new</span> <span style="color:blue;">byte</span>[blockSize];</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// There are two ways we could get the data. The simplest way</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// is to read the data, then immediately feed it to the output</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// stream using it&#8217;s Write feature (shown below, commented out.</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// The second way is to load the data into an array of bytes</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// (here implemented using the generic LIST). This would let</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// you manipulate the data in memory, then write it out (as</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// shown here), reupload it to another data stream, or do</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// something else entirely. </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// If you want to go the simple way, just remove all the</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// fileBytes lines and uncomment the outputStream line.</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">byte</span>&gt; fileBytes = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">byte</span>&gt;();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">int</span> bytesRead = databaseStream.Read(buffer, 0, buffer.Length);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">while</span> (bytesRead &gt; 0)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; bytesRead = databaseStream.Read(buffer, 0, buffer.Length);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">//outputStream.Write(buffer, 0, buffer.Length);</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">foreach</span> (<span style="color:blue;">byte</span> b <span style="color:blue;">in</span> buffer)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; fileBytes.Add(b);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Write out what is in the LIST to disk</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">foreach</span> (<span style="color:blue;">byte</span> b <span style="color:blue;">in</span> fileBytes)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">byte</span>[] barr = <span style="color:blue;">new</span> <span style="color:blue;">byte</span>[1];</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; barr[0] = b;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; outputStream.Write(barr, 0, 1);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Close the stream from the databaseStream</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; databaseStream.Close();</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Write out the file</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; outputStream.Close();</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Finally we should commit the transaction. </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; sqlCommand.Transaction.Commit();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">catch</span> (System.<span style="color:#2b91af;">Exception</span> ex)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">MessageBox</span>.Show(ex.ToString());</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">finally</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; sqlConnection.Close();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">return</span>;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160; }</p>
</p></div>
<p>The routine kicks off by opening a connection, then establishing a transaction. Remember from the previous lesson that every time you work with a FILESTREAM it has to be in a transaction. Next we basically duplicate the SQL used in the previous lesson, returning the path name, transaction context, and document name. The only difference is we pass in the ID as a parameter. With that, just like with the previous example we call the OpenSqlFilestream API. Note a difference, in this example the second parameter is “DESIRED_ACCESS_READ” as opposed to the write access we indicated previosly. </p>
<p>Once we have the “handle” we can create a FileStream for reading from the database. In this example I loop through the file stream, loading the data into a LIST of bytes. Once in memory we are free to work with it as we need to. In this example I simply loop back through the generic List and write the data to the file stream we opened on the disk for writing. If all you are doing is writing, it would be somewhat more efficient to write the code like so:</p>
<div style="font-family:consolas;background:white;color:black;font-size:10pt;">
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">int</span> bytesRead = databaseStream.Read(buffer, 0, buffer.Length);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">while</span> (bytesRead &gt; 0)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; bytesRead = databaseStream.Read(buffer, 0, buffer.Length);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; outputStream.Write(buffer, 0, buffer.Length);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Close the stream from the databaseStream</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; databaseStream.Close();</p>
</p></div>
<p>I simply eliminate the local byte array and write the buffer directly to the disk. Either way, the remainder is simple, just closing all the streams, commiting the transaction and closing the database connection. </p>
<p>This concludes the series on how to use FILESTREAM, in future posts we look into how to do Full Text Search with FILESTREAM stored objects. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/arcanecode.wordpress.com/1584/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/arcanecode.wordpress.com/1584/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/arcanecode.wordpress.com/1584/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/arcanecode.wordpress.com/1584/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/arcanecode.wordpress.com/1584/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/arcanecode.wordpress.com/1584/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/arcanecode.wordpress.com/1584/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/arcanecode.wordpress.com/1584/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/arcanecode.wordpress.com/1584/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/arcanecode.wordpress.com/1584/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/arcanecode.wordpress.com/1584/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/arcanecode.wordpress.com/1584/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/arcanecode.wordpress.com/1584/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/arcanecode.wordpress.com/1584/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1584&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://arcanecode.com/2009/05/27/accessing-filestream-data-from-a-client-net-application-part-2-downloading-a-file/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8436cd3ff437697b2608405422efa4da?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">arcanecode</media:title>
		</media:content>

		<media:content url="http://arcanecode.files.wordpress.com/2009/05/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Accessing FILESTREAM Data From A Client .NET Application &#8211; Part 1 Uploading a File</title>
		<link>http://arcanecode.com/2009/05/26/accessing-filestream-data-from-a-client-net-application-part-1-uploading-a-file/</link>
		<comments>http://arcanecode.com/2009/05/26/accessing-filestream-data-from-a-client-net-application-part-1-uploading-a-file/#comments</comments>
		<pubDate>Tue, 26 May 2009 06:02:00 +0000</pubDate>
		<dc:creator>arcanecode</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[C Sharp]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[FILESTREAM]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://arcanecode.wordpress.com/2009/05/26/accessing-filestream-data-from-a-client-net-application-part-1-uploading-a-file/</guid>
		<description><![CDATA[The best way to work with documents in a database is via a .Net application. I created a simple Windows forms project to access the table I created in previous lessons. I named the application FileLoader, you can the entire project at the Code Gallery site http://code.msdn.microsoft.com/FileStreamFTS . The interface is very simple: As you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1581&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The best way to work with documents in a database is via a .Net application. I created a simple Windows forms project to access the table I created in previous lessons. I named the application FileLoader, you can the entire project at the Code Gallery site <a href="http://code.msdn.microsoft.com/FileStreamFTS">http://code.msdn.microsoft.com/FileStreamFTS</a> . </p>
<p>The interface is very simple:</p>
<p><a href="http://arcanecode.files.wordpress.com/2009/05/image3.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://arcanecode.files.wordpress.com/2009/05/image_thumb3.png?w=495&#038;h=347" width="495" height="347" /></a> </p>
<p>As you can see there are two main functions, the upper half uploads a file to the SQL Server. The lower half displays the files already in the table, lets the user pick one and then click the GetFile button to save it locally. Today we’ll look at the Upload File functionality. Here is the code:</p>
<div style="font-family:consolas;background:white;color:black;font-size:10pt;">
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">private</span> <span style="color:blue;">void</span> btnUploadFile_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;">&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Reset in case it was used previously</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; lblStatus.Text = <span style="color:#a31515;">&quot;&quot;</span>;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Make sure user entered something</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span> (txtFile.Text.Length == 0)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">&quot;Must supply a file name&quot;</span>);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; lblStatus.Text = <span style="color:#a31515;">&quot;Must supply file name&quot;</span>;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">return</span>;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Make sure what user entered is valid</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">FileInfo</span> fi = <span style="color:blue;">new</span> <span style="color:#2b91af;">FileInfo</span>(txtFile.Text);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span> (!fi.Exists)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">&quot;The file you entered does not exist.&quot;</span>);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; lblStatus.Text = <span style="color:#a31515;">&quot;The file you entered does not exist.&quot;</span>;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">return</span>;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Upload the file to the database</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">FileTransfer</span>.UploadFile(txtFile.Text);</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Refresh the datagrid to show the newly added file</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; LoadDataGridView();</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Let user know it was uploaded</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; lblStatus.Text = fi.Name + <span style="color:#a31515;">&quot; Uploaded&quot;</span>;</p>
<p style="margin:0;">&#160;&#160;&#160; }</p>
</p></div>
<p>The real line of importance is the FileTransfer.UploadFile. This calls a static method in a class I named FileTransfer.cs. In order to use FILESTREAM there is an API call we have to make, so at the header area of the FileTransfer we have a lot of declarations. These are pretty much a straight copy from the MSDN help files.</p>
<div style="font-family:consolas;background:white;color:black;font-size:10pt;">
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//These contants are passed to the OpenSqlFilestream()</span></p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//API DesiredAccess parameter. They define the type</span></p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//of BLOB access that is needed by the application.</span></p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">const</span> <span style="color:#2b91af;">UInt32</span> DESIRED_ACCESS_READ = 0&#215;00000000;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">const</span> <span style="color:#2b91af;">UInt32</span> DESIRED_ACCESS_WRITE = 0&#215;00000001;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">const</span> <span style="color:#2b91af;">UInt32</span> DESIRED_ACCESS_READWRITE = 0&#215;00000002;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//These contants are passed to the OpenSqlFilestream()</span></p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//API OpenOptions parameter. They allow you to specify</span></p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//how the application will access the FILESTREAM BLOB</span></p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//data. If you do not want this ability, you can pass in</span></p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//the value 0. In this code sample, the value 0 has</span></p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//been defined as SQL_FILESTREAM_OPEN_NO_FLAGS.</span></p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">const</span> <span style="color:#2b91af;">UInt32</span> SQL_FILESTREAM_OPEN_NO_FLAGS = 0&#215;00000000;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">const</span> <span style="color:#2b91af;">UInt32</span> SQL_FILESTREAM_OPEN_FLAG_ASYNC = 0&#215;00000001;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">const</span> <span style="color:#2b91af;">UInt32</span> SQL_FILESTREAM_OPEN_FLAG_NO_BUFFERING = 0&#215;00000002;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">const</span> <span style="color:#2b91af;">UInt32</span> SQL_FILESTREAM_OPEN_FLAG_NO_WRITE_THROUGH = 0&#215;00000004;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">const</span> <span style="color:#2b91af;">UInt32</span> SQL_FILESTREAM_OPEN_FLAG_SEQUENTIAL_SCAN = 0&#215;00000008;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">const</span> <span style="color:#2b91af;">UInt32</span> SQL_FILESTREAM_OPEN_FLAG_RANDOM_ACCESS = 0&#215;00000010;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//This structure defines the format of the final parameter to the</span></p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//OpenSqlFilestream() API.</span></p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//This statement imports the OpenSqlFilestream API so that it</span></p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//can be called in the Main() method below.</span></p>
<p style="margin:0;">&#160;&#160;&#160; [<span style="color:#2b91af;">DllImport</span>(<span style="color:#a31515;">&quot;sqlncli10.dll&quot;</span>, SetLastError = <span style="color:blue;">true</span>, CharSet = <span style="color:#2b91af;">CharSet</span>.Unicode)]</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">static</span> <span style="color:blue;">extern</span> <span style="color:#2b91af;">SafeFileHandle</span> OpenSqlFilestream(</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">string</span> Filestreamath,</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">uint</span> DesiredAccess,</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">uint</span> OpenOptions,</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">byte</span>[] FilestreamTransactionContext,</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">uint</span> FilestreamTransactionContextLength,</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">Int64</span> AllocationSize);</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//This statement imports the Win32 API GetLastError().</span></p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//This is necessary to check whether OpenSqlFilestream</span></p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:green;">//succeeded in returning a valid / handle</span></p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160; [<span style="color:#2b91af;">DllImport</span>(<span style="color:#a31515;">&quot;kernel32.dll&quot;</span>, SetLastError = <span style="color:blue;">true</span>)]</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">static</span> <span style="color:blue;">extern</span> <span style="color:#2b91af;">UInt32</span> GetLastError();</p>
</p></div>
<p>OK, with that out of the way, I’ve created a public, static method to upload the file. Here is the full routine:</p>
<div style="font-family:consolas;background:white;color:black;font-size:10pt;">
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> UploadFile(<span style="color:blue;">string</span> fileName)</p>
<p style="margin:0;">&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Establish db connection</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">SqlConnection</span> sqlConnection = <span style="color:blue;">new</span> <span style="color:#2b91af;">SqlConnection</span>(</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#a31515;">&quot;Integrated Security=true;server=(local)&quot;</span>);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">SqlTransaction</span> transaction = <span style="color:blue;">null</span>;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Create a File Info object so you can easily get the</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// name and extenstion. As an alternative you could</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// choose to pass them in,&#160; or use some other way</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// to extract the extension and name. </span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">FileInfo</span> fi = <span style="color:blue;">new</span> <span style="color:#2b91af;">FileInfo</span>(fileName);</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">try</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Open the file as a stream</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">FileStream</span> sourceFile = <span style="color:blue;">new</span> <span style="color:#2b91af;">FileStream</span>(fileName</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , <span style="color:#2b91af;">FileMode</span>.OpenOrCreate, <span style="color:#2b91af;">FileAccess</span>.Read);</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Create the row in the database</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; sqlConnection.Open();</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">SqlCommand</span> cmd = <span style="color:blue;">new</span> <span style="color:#2b91af;">SqlCommand</span>();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; cmd.Connection = sqlConnection;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; cmd.CommandText = <span style="color:#a31515;">&quot;INSERT INTO &quot;</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="color:#a31515;">&quot;FileStreamFTS.dbo.DocumentRepository&quot;</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="color:#a31515;">&quot;(DocumentExtension, DocumentName) VALUES (&#8216;&quot;</span> </p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + fi.Extension + <span style="color:#a31515;">&quot;&#8217;, &#8216;&quot;</span> </p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + fi.Name + <span style="color:#a31515;">&quot;&#8217;)&quot;</span>;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; cmd.ExecuteNonQuery();</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Now upload the file. It must be done inside a transaction.</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; transaction = sqlConnection.BeginTransaction(<span style="color:#a31515;">&quot;mainTranaction&quot;</span>);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; cmd.Transaction = transaction;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; cmd.CommandText = <span style="color:#a31515;">&quot;SELECT Document.PathName(), &quot;</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="color:#a31515;">&quot;GET_FILESTREAM_TRANSACTION_CONTEXT() &quot;</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="color:#a31515;">&quot;FROM FileStreamFTS.dbo.DocumentRepository &quot;</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + <span style="color:#a31515;">&quot;WHERE ID=(select max(id) from FileStreamFTS.dbo.DocumentRepository)&quot;</span>;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">SqlDataReader</span> rdr = cmd.ExecuteReader();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span> (rdr.Read() == <span style="color:blue;">false</span>)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">throw</span> <span style="color:blue;">new</span> <span style="color:#2b91af;">Exception</span>(<span style="color:#a31515;">&quot;Could not get file stream context&quot;</span>);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Get the path</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">string</span> path = (<span style="color:blue;">string</span>)rdr[0];</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Get a file stream context</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">byte</span>[] context = (<span style="color:blue;">byte</span>[])rdr[1];</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">int</span> length = context.Length;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; rdr.Close();</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Now use the API to get a reference (handle) to the filestream</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">SafeFileHandle</span> handle = OpenSqlFilestream(path</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , DESIRED_ACCESS_WRITE</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , SQL_FILESTREAM_OPEN_NO_FLAGS</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , context, (<span style="color:#2b91af;">UInt32</span>)length, 0);</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Now create a true .Net filestream to the database</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// using the handle we got in the step above</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">FileStream</span> dbStream = <span style="color:blue;">new</span> <span style="color:#2b91af;">FileStream</span>(handle, <span style="color:#2b91af;">FileAccess</span>.Write);</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Setup a buffer to hold the data we read from disk</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">int</span> blocksize = 1024 * 512;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">byte</span>[] buffer = <span style="color:blue;">new</span> <span style="color:blue;">byte</span>[blocksize];</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Read from file and write to DB</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">int</span> bytesRead = sourceFile.Read(buffer, 0, buffer.Length);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">while</span> (bytesRead &gt; 0)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dbStream.Write(buffer, 0, buffer.Length);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; bytesRead = sourceFile.Read(buffer, 0, buffer.Length);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">// Done reading, close all of our streams and commit the file</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; dbStream.Close();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; sourceFile.Close();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; transaction.Commit();</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">catch</span> (<span style="color:#2b91af;">Exception</span> e)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span> (transaction != <span style="color:blue;">null</span>)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; transaction.Rollback();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">throw</span> e;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">finally</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; sqlConnection.Close();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160; }</p>
</p></div>
<p>First we open a connection to the SQL Server, then create a FileInfo object to make it simple to extract the file name and extension. Next a record is inserted into the database that will act as a place holder. It has the name of the file and the extension, but no file yet. I did go ahead and open a FileStream to the source file, located on the disk. We’ll need this later to upload the file. </p>
<p>Next you will see that I begin a transaction. Every time you work with a FILESTREAM it must always be in the context of a transaction. After that a SQL Data Reader is created that has three pieces of information. First, it calls the PathName() function for the Document field in our table. The PathName() will be needed later when we call the API. The second field is returned from the GET_FILESTREAM_TRANSACTION_CONTEXT function, and returns the transaction context for the transaction. Note this is not the name (in this example “mainTransaction”), but the context which is a special value. These two values are then copied into local variables which will be used in calling the OpenSqlFilestream API. In this example I also retrieve the DocumentName field, this is used by the code when it writes the file to the database, but is not strictly needed for the FILESTREAM. </p>
<p>Next you will see the call to the OpenSqlFilestream API, which returns a “handle”. This handle is then used to create a FileStream object. Using this newly created FileStream (here named dbStream) we can then upload the file. Now the main work begins. After setting up a buffer, we then simply read from the source file stream into the buffer, then write the exact same buffer to the database FileStream. The loop continues until there are no more bytes in the source. </p>
<p>At this point we are essentially done. We close the streams, commit the transaction, and in the finally block close the SQL database connection. The file should now be in the database. I do want to point out one thing. In the SQL to get the information to the row just uploaded, I use a subquery to get the max(id), essentially returning the last row just inserted. This is fine for this simple example, when the database has just one user. In your production systems where you are likely to have many users, however, you should use an alternate method to return the row you need. Otherwise two users could insert rows at the same time, and thus a conflict could occur with both of them getting back the same max(id). It will not happen often, but at some point it could happen and be very hard to debug. </p>
<p>This handled the uploading of files to the SQL Server via FILESTREAM, in the next installment we’ll look at how to retrieve the file we just uploaded. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/arcanecode.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/arcanecode.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/arcanecode.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/arcanecode.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/arcanecode.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/arcanecode.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/arcanecode.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/arcanecode.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/arcanecode.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/arcanecode.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/arcanecode.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/arcanecode.wordpress.com/1581/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/arcanecode.wordpress.com/1581/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/arcanecode.wordpress.com/1581/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1581&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://arcanecode.com/2009/05/26/accessing-filestream-data-from-a-client-net-application-part-1-uploading-a-file/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8436cd3ff437697b2608405422efa4da?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">arcanecode</media:title>
		</media:content>

		<media:content url="http://arcanecode.files.wordpress.com/2009/05/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating Tables and Inserting Rows With FILESTREAM</title>
		<link>http://arcanecode.com/2009/05/20/creating-tables-and-inserting-rows-with-filestream/</link>
		<comments>http://arcanecode.com/2009/05/20/creating-tables-and-inserting-rows-with-filestream/#comments</comments>
		<pubDate>Wed, 20 May 2009 06:09:00 +0000</pubDate>
		<dc:creator>arcanecode</dc:creator>
				<category><![CDATA[FILESTREAM]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://arcanecode.wordpress.com/2009/05/20/creating-tables-and-inserting-rows-with-filestream/</guid>
		<description><![CDATA[In previous lessons, we setup the server to handle FILESTREAM, then we looked at how to create or alter a database to work with FILESTREAM. In this lesson we will create a table to store a FILESTREAM, then insert a few rows into it. Before we create our table, be aware of some requirements necessary [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1576&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In previous lessons, we setup the server to handle FILESTREAM, then we looked at how to create or alter a database to work with FILESTREAM. In this lesson we will create a table to store a FILESTREAM, then insert a few rows into it. </p>
<p>Before we create our table, be aware of some requirements necessary for FILESTREAM to work. First, you must have a special column that FILESTREAM uses to uniquely identify the stream. It must be a unique non null identifier of type ROWGUIDCOL. If we specify a default of NEWSEQUENTIALID the column becomes self maintaining. When we insert a new value into the row, SQL Server takes care of creating a GUID for us and we essentially can ignore the column. Here is an example:</p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=USE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">USE</a> FileStreamFTS
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=GO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">GO</a>
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=CREATE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">CREATE</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=TABLE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">TABLE</a> DocumentRepository(
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">  ID <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=IDENTITY&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">IDENTITY</a>(1,1) <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=NOT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">NOT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=NULL&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">NULL</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=PRIMARY&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">PRIMARY</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=KEY&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">KEY</a>
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">, FileStreamID <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=UNIQUEIDENTIFIER&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">UNIQUEIDENTIFIER</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=ROWGUIDCOL&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">ROWGUIDCOL</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=NOT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">NOT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=NULL&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">NULL</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=UNIQUE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">UNIQUE</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=DEFAULT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">DEFAULT</a> NEWSEQUENTIALID()
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">, DocumentExtension <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=VARCHAR&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">VARCHAR</a>(10)
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">, DocumentName <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=VARCHAR&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">VARCHAR</a>(256)
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">, Document <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=VARBINARY&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">VARBINARY</a>(<a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=MAX&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">MAX</a>) FILESTREAM <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=DEFAULT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">DEFAULT</a>(0x)
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">);
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=GO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">GO</a>
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"></pre>
<p>Here the column “FileStreamID” will become the required column for FILESTREAM. Note the column name is not important, I could have called it “FSID”, “FSIdentity”, or even “Spock”. You’ll also note I created an ID column for our use in normal day to day operations. This is not a requirement of FILESTREAM, just good practice. There is a second requirement however. For the column that will be storing the documents, it must be VARBINARY(MAX), and it must add the FILESTREAM clause. </p>
<p>You will also note the default of “0x” (hex 0). This will be important if you wish to insert a new row without supplying the document at the time the row is created. It will create a file to act as a placeholder until such time as the real document is supplied. </p>
<p>You can also alter an existing table to add FILESTREAM capabilities. Simply use the ALTER TABLE command, add the unique identifier column (in this example “FileStreamID”) and the VARBINARY(MAX) column to hold your data (“Document” in the above example). </p>
<p>It’s time to insert some data. Normally the best way to insert data is using a client application, such as something written in .Net. It is possible to add documents though via T-SQL. To supply data for these examples, I decided a little culture was in order. I went to <a href="http://shakespeare.mit.edu/">http://shakespeare.mit.edu/</a> and copied some of the plays into Microsoft Word documents. Note I used the older “.doc” format from 2003, not the 2007 “.docx”. This is not particularly important as far as FILESTREAM is concerned, but will come into play in later lessons when we look at doing Full Text Searches on this data. </p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INSERT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INSERT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INTO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INTO</a> DocumentRepository(DocumentExtension, DocumentName, Document)
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=SELECT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">SELECT</a>
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"> '<span style="color:#8b0000;">doc</span>' <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=AS&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">AS</a> DocumentExtension
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"> , '<span style="color:#8b0000;">Hamlet.doc</span>' <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=AS&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">AS</a> DocumentName
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"> , * <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=FROM&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">FROM</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=OPENROWSET&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">OPENROWSET</a>(<a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=BULK&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">BULK</a> '<span style="color:#8b0000;">D:\Docs\Hamlet.doc</span>', SINGLE_BLOB)
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">   <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=AS&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">AS</a> Document;
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=GO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">GO</a>
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"></pre>
<p>Here we’ve inserted a new row into the table, and have ignored the “ID” and “FileStreamID” letting SQL Server create the values. The “DocumentExtension” and “DocumentName” columns are straightforward. To supply the document however, we need to use an OPENROWSET. This will supply the data from a disk file as a single BLOB (Binary Large OBject). </p>
<p>Let’s verify what was just inserted with this query:</p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=SELECT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">SELECT</a> ID
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">     , FileStreamID
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">     , DocumentExtension <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=AS&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">AS</a> Ext
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">     , DocumentName
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">     , <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=CAST&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">CAST</a>(Document <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=AS&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">AS</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=VARCHAR&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">VARCHAR</a>) <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> DocumentData
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=FROM&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">FROM</a> DocumentRepository;
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"></pre>
<p>Notice the CAST, its necessary in order to get a view into the stored document. </p>
<table border="1" cellspacing="0" cellpadding="2" width="617">
<tbody>
<tr>
<td valign="top" width="23">ID </td>
<td valign="top" width="327">FileStreamID </td>
<td valign="top" width="42">Ext </td>
<td valign="top" width="104">DocumentName </td>
<td valign="top" width="119">DocumentData</td>
</tr>
<tr>
<td valign="top" width="23">1</td>
<td valign="top" width="327">A54A8FFE-F742-DE11-AC61-002243CE0AAB </td>
<td valign="top" width="42">doc </td>
<td valign="top" width="104">Hamlet.doc</td>
<td valign="top" width="119">ÐÏà¡±á &gt; þÿ </td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<p>As mentioned previously, it is also possible to insert a new row, then add the document later. To insert a new row into our example table, we simply use:</p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INSERT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INSERT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INTO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INTO</a> DocumentRepository(DocumentExtension, DocumentName)
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=VALUES&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">VALUES</a> ('<span style="color:#8b0000;">doc</span>', '<span style="color:#8b0000;">AMidsummerNightsDream.doc</span>');
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=GO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">GO</a>
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"></pre>
<p>Now that the row exists, we can update it.</p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=UPDATE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">UPDATE</a>  DocumentRepository
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=SET&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">SET</a>     Document = ( <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=SELECT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">SELECT</a> *
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">                     <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=FROM&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">FROM</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=OPENROWSET&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">OPENROWSET</a>(
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">                      <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=BULK&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">BULK</a> '<span style="color:#8b0000;">D:\Docs\AMidsummerNightsDream.doc</span>',
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">                      SINGLE_BLOB) <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=AS&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">AS</a> TheDocument
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">                   )
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=WHERE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">WHERE</a>   ID = 2 ;
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=GO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">GO</a>
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"></pre>
<p>While it is possible to add data in this format, it’s fairly rare. The majority of the time you will want to use a client side application, which is what we’ll look at in the next installment of this series. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/arcanecode.wordpress.com/1576/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/arcanecode.wordpress.com/1576/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/arcanecode.wordpress.com/1576/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/arcanecode.wordpress.com/1576/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/arcanecode.wordpress.com/1576/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/arcanecode.wordpress.com/1576/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/arcanecode.wordpress.com/1576/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/arcanecode.wordpress.com/1576/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/arcanecode.wordpress.com/1576/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/arcanecode.wordpress.com/1576/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/arcanecode.wordpress.com/1576/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/arcanecode.wordpress.com/1576/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/arcanecode.wordpress.com/1576/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/arcanecode.wordpress.com/1576/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1576&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://arcanecode.com/2009/05/20/creating-tables-and-inserting-rows-with-filestream/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8436cd3ff437697b2608405422efa4da?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">arcanecode</media:title>
		</media:content>
	</item>
		<item>
		<title>Enabling FILESTREAM In The Database</title>
		<link>http://arcanecode.com/2009/05/19/enabling-filestream-in-the-database/</link>
		<comments>http://arcanecode.com/2009/05/19/enabling-filestream-in-the-database/#comments</comments>
		<pubDate>Tue, 19 May 2009 06:09:00 +0000</pubDate>
		<dc:creator>arcanecode</dc:creator>
				<category><![CDATA[FILESTREAM]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://arcanecode.wordpress.com/2009/05/19/enabling-filestream-in-the-database/</guid>
		<description><![CDATA[Once you have your server configured for FILESTREAM, you will need to configure your database. The easiest way is to establish it when you first create the database. CREATE DATABASE FileStreamFTS ON PRIMARY ( NAME = FileStreamFTS_Data, FILENAME = 'd:\data\FileStreamFTS_Data.mdf'), FILEGROUP FileStreamFTS_FileGroup1 CONTAINS FILESTREAM( NAME = FileStreamFTS_FileGroup, FILENAME = 'd:\data\FileStreamFTS_FileGroup') LOG ON ( NAME = [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1573&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Once you have your <a href="http://arcanecode.com/2009/05/18/enabling-filestream-on-sql-server-2008/">server configured</a> for FILESTREAM, you will need to configure your database. The easiest way is to establish it when you first create the database. </p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=CREATE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">CREATE</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=DATABASE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">DATABASE</a> FileStreamFTS <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=ON&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">ON</a>
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=PRIMARY&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">PRIMARY</a> ( NAME = FileStreamFTS_Data,
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">    FILENAME = '<span style="color:#8b0000;">d:\data\FileStreamFTS_Data.mdf</span>'),
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">FILEGROUP FileStreamFTS_FileGroup1
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">  <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=CONTAINS&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">CONTAINS</a> FILESTREAM( NAME = FileStreamFTS_FileGroup,
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">    FILENAME = '<span style="color:#8b0000;">d:\data\FileStreamFTS_FileGroup</span>')
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">LOG <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=ON&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">ON</a>  ( NAME = FileStreamFTS_Log,
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">    FILENAME = '<span style="color:#8b0000;">d:\data\FileStreamFTS_Log.ldf</span>')
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=GO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">GO</a></pre>
<p>In the above example, I created my database in a directory on my D drive named “D:\Data”. If you examine the folder you ’ll now see two files, the mdf and ldf files. There is also a folder “FileStreamFTS_FileGroup”. It is in this folder that your files will eventually be stored in. </p>
<p>You may be tempted to poking around in the folder, and perhaps even access the stored files directly. My advice to you: don’t. Microsoft has strongly advised against this practice. After all, you went to all the trouble of setting up FILESTREAM to let SQL Server handle this for you, so let it do the handling. If you are curious to dig deeper into how FILESTREAM works behind the scenes, I would recommend Paul Randal’s excellent white paper available at <a href="http://msdn.microsoft.com/en-us/library/cc949109.aspx">http://msdn.microsoft.com/en-us/library/cc949109.aspx</a>. </p>
<p>The above example shows how to add FILESTREAM capability when you first create your database. That’s great, but what if you already have an existing database you want to add FILESTREAM to? I have just such an existing database, “ArcaneCode”. To add FILESTREAM I need to issue two ALTER DATABASE statements. First, we need to add a FILEGROUP and indicate that file group contains FILESTREAM objects. </p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=ALTER&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">ALTER</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=DATABASE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">DATABASE</a> ArcaneCode
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=ADD&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">ADD</a> FILEGROUP ArcaneFileStreamGroup1
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">  <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=CONTAINS&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">CONTAINS</a> FILESTREAM
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=GO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">GO</a></pre>
<p>In the second we have to indicate to the FILEGROUP where the FILESTREAM should be stored. Pass in the directory name where you want your files saved as the FILENAME parameter. As with the create statement the directory can not exist, if it does you’ll get an error. </p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=ALTER&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">ALTER</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=DATABASE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">DATABASE</a> ArcaneCode
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=ADD&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">ADD</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=FILE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">FILE</a>
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">  (NAME = '<span style="color:#8b0000;">ArcaneFS_Group</span>'
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">   , FILENAME = '<span style="color:#8b0000;">D:\Data\ArcaneCodeFS</span>'
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;">   )
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=TO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">TO</a> FILEGROUP ArcaneFileStreamGroup1
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:11px;margin:0;"><a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=GO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">GO</a></pre>
<p>This completes the steps needed to create a database with FILESTREAM, or add it to an existing database. In the next lesson we will look at creating tables that use FILESTREAM.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/arcanecode.wordpress.com/1573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/arcanecode.wordpress.com/1573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/arcanecode.wordpress.com/1573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/arcanecode.wordpress.com/1573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/arcanecode.wordpress.com/1573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/arcanecode.wordpress.com/1573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/arcanecode.wordpress.com/1573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/arcanecode.wordpress.com/1573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/arcanecode.wordpress.com/1573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/arcanecode.wordpress.com/1573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/arcanecode.wordpress.com/1573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/arcanecode.wordpress.com/1573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/arcanecode.wordpress.com/1573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/arcanecode.wordpress.com/1573/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1573&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://arcanecode.com/2009/05/19/enabling-filestream-in-the-database/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8436cd3ff437697b2608405422efa4da?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">arcanecode</media:title>
		</media:content>
	</item>
		<item>
		<title>Enabling FILESTREAM on SQL Server 2008</title>
		<link>http://arcanecode.com/2009/05/18/enabling-filestream-on-sql-server-2008/</link>
		<comments>http://arcanecode.com/2009/05/18/enabling-filestream-on-sql-server-2008/#comments</comments>
		<pubDate>Mon, 18 May 2009 06:05:00 +0000</pubDate>
		<dc:creator>arcanecode</dc:creator>
				<category><![CDATA[FILESTREAM]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://arcanecode.wordpress.com/2009/05/18/enabling-filestream-on-sql-server-2008/</guid>
		<description><![CDATA[One of the newly touted features of SQL Server 2008 is file streaming. For some time now SQL Server has allowed users to store large binary objects in the database inside a varbinary(max) field. Performance began to suffer, however, when the binary object was over 1 megabyte or so in size. SQL Server 2008 solves [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1571&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the newly touted features of SQL Server 2008 is <i>file streaming</i>. For some time now SQL Server has allowed users to store large binary objects in the database inside a varbinary(max) field. Performance began to suffer, however, when the binary object was over 1 megabyte or so in size. </p>
<p>SQL Server 2008 solves this through its new FILESTREAM feature. With FILESTREAM SQL Server lets the operating system, Windows Server, do what it does best: handle the storage of binary objects, better known as files. In the database, SQL Server simply stores a reference that will let it open and close the file from the disk. FILESTREAM gives us the best of all worlds. When the file group is backed up so are the files, they can also be made part of a transaction. </p>
<p>By default, FILESTREAM is not enabled when you install SQL Server 2008. Activating it is a simple, two step process. As always though make sure to do proper backups before making any changes to your SQL Server, and be sure to first implement on development, then test systems before making changes to any production server. </p>
<p>First, you will need to enable FILESTREAM for the Windows Server it is running on. Begin by opening the SQL Server Configuration Manager. Once inside click on “SQL Server Services” then highlight the server you want to activate. In this example we clicked on “SQL Server (MSSQLSERVER)”. </p>
<p><a href="http://arcanecode.files.wordpress.com/2009/05/image.png"><img style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" title="image" border="0" alt="image" src="http://arcanecode.files.wordpress.com/2009/05/image_thumb.png?w=584&#038;h=325" width="584" height="325" /></a> </p>
<p>Now that it is highlighted, right click on it and pick properties from the menu. Click on the FILESTREAM tab in the dialog that appears. </p>
<p><a href="http://arcanecode.files.wordpress.com/2009/05/image1.png"><img style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" title="image" border="0" alt="image" src="http://arcanecode.files.wordpress.com/2009/05/image_thumb1.png?w=413&#038;h=456" width="413" height="456" /></a> </p>
</p>
<p>If you have a default install, your dialog should appear with nothing enabled, as this one does. Check on the first line, “Enable FILESTREAM for Trancact-SQL access”. This turns on the FILESTREAM feature, but with a drawback. It only works from T-SQL, and then only on the server itself. To make it useful we need to go further.</p>
<p>Once the first line is checked on, the second line, “Enable FILESTREAM for file I/O streaming access” will become enabled. Check it on. At this point the server will allow FILESTREAM from a separate application, but only when run on the same Windows Server that SQL Server is running on. </p>
<p>Since most of your users will not be running on the SQL Server itself, you will want to check on the final option which is now available, “Allow remote clients to have streaming access to FILESTREAM data”. Once this is checked, you can click OK to save your changes. </p>
<p>Note there may be one exception to checking on the final option. If you are running SQL Server Express, it is quite possible your application and the database may all be running on the same computer, and that you do not want any other machine to get access to this database. While this would be an uncommon situation, please note that the capability does exist for you to set things up this way if needed. </p>
<p>Now that the Windows Server is ready to handle FILESTREAM we need to enable the SQL Server. Fortunately this winds up being a simple matter. Open up SQL Server Management Studio, and enter the following command:</p>
<pre></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:10px;margin:0;">  <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=EXEC&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">EXEC</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=sp_configure&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">sp_configure</a> filestream_access_level, 2
</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,&#39;font-size:10px;margin:0;">  <a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=RECONFIGURE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">RECONFIGURE</a></pre>
</p>
<p>Note the number at the end of the first line. Valid values for this are “0”, “1”, and “2”. “0” will disable FILESTREAM completely. “1” will enable it, but only for T-SQL code. “2” will fully enable it and allow access via .NET or other code. Note that these are slightly different from the way the Windows Server rights are set. Here, in order to run anything other than T-SQL you must use a “2”, regardless of where the code is executed (local or remote). </p>
<p>It is also possible to change this setting in the GUI. Right click on the server in SQL Server Management Studio, in this case it is “(local)”, and pick “Properties” from the menu. Go to the “Advanced” page and the FILESTREAM is the top most item, you can see it here with all of its options. </p>
<p><a href="http://arcanecode.files.wordpress.com/2009/05/image2.png"><img style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" title="image" border="0" alt="image" src="http://arcanecode.files.wordpress.com/2009/05/image_thumb2.png?w=632&#038;h=140" width="632" height="140" /></a>&#160;</p>
<p>Now your server has been properly configured to handle file streaming, you’ll want to create a database in a way to handle file streaming. We’ll do that in the next installment in this series. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/arcanecode.wordpress.com/1571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/arcanecode.wordpress.com/1571/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/arcanecode.wordpress.com/1571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/arcanecode.wordpress.com/1571/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/arcanecode.wordpress.com/1571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/arcanecode.wordpress.com/1571/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/arcanecode.wordpress.com/1571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/arcanecode.wordpress.com/1571/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/arcanecode.wordpress.com/1571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/arcanecode.wordpress.com/1571/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/arcanecode.wordpress.com/1571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/arcanecode.wordpress.com/1571/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/arcanecode.wordpress.com/1571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/arcanecode.wordpress.com/1571/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=arcanecode.com&amp;blog=340781&amp;post=1571&amp;subd=arcanecode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://arcanecode.com/2009/05/18/enabling-filestream-on-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8436cd3ff437697b2608405422efa4da?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">arcanecode</media:title>
		</media:content>

		<media:content url="http://arcanecode.files.wordpress.com/2009/05/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://arcanecode.files.wordpress.com/2009/05/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://arcanecode.files.wordpress.com/2009/05/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
	</channel>
</rss>
