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

<channel>
	<title>Dot Net Thoughts &#187; FileStream</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/filestream/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Writing File with Non Cache mode in C#</title>
		<link>http://www.dotnetthoughts.net/2010/01/19/writing_file_with_non_cache_mode_in_c/</link>
		<comments>http://www.dotnetthoughts.net/2010/01/19/writing_file_with_non_cache_mode_in_c/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 14:39:16 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[FileStream]]></category>
		<category><![CDATA[NoCache]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=733</guid>
		<description><![CDATA[Normally when we are writing to the File Systems or I/O devices, Windows will cache the request or response to get better performance. This behavior is a good feature, but sometimes we require immediate change. By caching Windows sometimes mislead us. And I don’t think there is a way to avoid this option available in [...]]]></description>
			<content:encoded><![CDATA[<p>Normally when we are writing to the File Systems or I/O devices, Windows will cache the request or response to get better performance. This behavior is a good feature, but sometimes we require immediate change. By caching Windows sometimes mislead us. And I don’t think there is a way to avoid this option available in Windows. In my current project I got a chance to explore / work on this, but I need to avoid this caching. <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  I checked various options with File class and Stream class but there was no option available to avoid Caching. Later our VC++ developer gives me some code, which in WIN32 API, which will avoid caching. It was using &#8220;<em>CreateFile()</em>&#8221; method in Kernal32.dll with <em>FILE_FLAG_NO_BUFFERING</em> option. Then I was able to create same in C# code base on the input using PInvoke. But I have to find a managed code, with that we can write / read stream without caching. Later I found FileStream class, which supporting both synchronous and asynchronous read and write operations. But it also doesn’t have a NonCahce file option. Then I tried FileStream class with <em>FILE_FLAG_NO_BUFFERING</em> option. And it worked <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre class="brush: csharp;">
const FileOptions FILE_FLAG_NO_BUFFERING = (FileOptions) 0x20000000;

using(FileStream fs = new FileStream(&quot;Path&quot;,FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1024, FileOptions.WriteThrough | FILE_FLAG_NO_BUFFERING))
{
fs.Write(&quot;HelloWorld&quot;);
}
</pre>
<p>This FileStream class also got a nice option, <em>FileOptions.DeleteOnClose</em>, if you are creating a File with this option enabled, it will delete the File after you close the FileStream. This can be used for creating real temporary files.</p>
<pre class="brush: csharp;">
string TempFile = Path.GetTempFileName();
using(FileStream fs = new FileStream(TempFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1024, FileOptions.DeleteOnClose))
{
fs.Write(&quot;HelloWorld&quot;);
} //Deletes the File.
</pre>
<p>I think this FileStream class is available from .Net 2.0 Framework onwards.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/01/19/writing_file_with_non_cache_mode_in_c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FILESTREAM in SQL Server 2008</title>
		<link>http://www.dotnetthoughts.net/2009/07/06/filestream-in-sql-server-2008/</link>
		<comments>http://www.dotnetthoughts.net/2009/07/06/filestream-in-sql-server-2008/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 08:16:42 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[FileStream]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=310</guid>
		<description><![CDATA[SQL Server 2008 comes with lots of new features compared to the previous versions of SQL Server. One of the new feature is FileStream, which allows storage of and efficient access to BLOB data using a combination of SQL Server 2008 and the NTFS file system. You can get more details about this in MSDN [...]]]></description>
			<content:encoded><![CDATA[<p>SQL Server 2008 comes with lots of new features compared to the previous versions of SQL Server. One of the new feature is FileStream, which allows storage of and efficient access to BLOB data using a combination of SQL Server 2008 and the NTFS file system.</p>
<p>You can get more details about this in MSDN : <a href="http://msdn.microsoft.com/en-us/library/cc949109.aspx" target="_blank">FILESTREAM Storage in SQL Server 2008</a></p>
<p><strong>Enable Filestream in SQL Server</strong></p>
<p>By default the Filestream feature will be disabled. You can enable the filestream using SQL Server Configuration Manager under SQL Server 2008 &gt; Configuration Tools. In this you will get all the SQL Server services. Select the Properties of the instance and select the Tab &#8220;FileStream&#8221;, from that you can enable the FileStream, you can also specifiy the instance name also.</p>
<div id="attachment_313" class="wp-caption alignnone" style="width: 415px"><a href="http://anuraj.files.wordpress.com/2009/07/enable_filestream1.jpg"><img class="size-full wp-image-313" title="How to enable FileStream" src="http://anuraj.files.wordpress.com/2009/07/enable_filestream1.jpg" alt="How to enable FileStream" width="405" height="446" /></a><p class="wp-caption-text">How to enable FileStream</p></div>
<p>You can also do it via T-SQL statement also</p>
<pre class="brush: sql;">
EXEC sys.sp_configure N'filestream access level', N'2'
RECONFIGURE
</pre>
<p>After doing this SQL Server will create a shared folder in your machine(or in Server) with the instance name specified. (Or it will create the Windows Share name we are specifying in the textbox) Only SQL Server can access the contents.</p>
<div id="attachment_314" class="wp-caption alignnone" style="width: 314px"><a href="http://anuraj.files.wordpress.com/2009/07/shared1.jpg"><img class="size-full wp-image-314" title="Network " src="http://anuraj.files.wordpress.com/2009/07/shared1.jpg" alt="Network " width="304" height="243" /></a><p class="wp-caption-text">Network </p></div>
<p>You can check this via command prompt, using &#8220;Net Share&#8221;, you will get an output like this.</p>
<div id="attachment_318" class="wp-caption alignnone" style="width: 490px"><a href="http://anuraj.files.wordpress.com/2009/07/command_net_share.jpg"><img src="http://anuraj.files.wordpress.com/2009/07/command_net_share.jpg" alt="Net share command output" title="Net share command" width="480" height="241" class="size-full wp-image-318" /></a><p class="wp-caption-text">Net share command output</p></div>
<p><strong>Using Filestream in the Database.</strong></p>
<p>For using Filestream in your database you have to add file group in New Database screen.</p>
<div id="attachment_319" class="wp-caption alignnone" style="width: 489px"><a href="http://anuraj.files.wordpress.com/2009/07/filestream_demo.jpg"><img src="http://anuraj.files.wordpress.com/2009/07/filestream_demo.jpg" alt="Enable filestream for new Database" title="Enable filestream for new Database" width="479" height="430" class="size-full wp-image-319" /></a><p class="wp-caption-text">Enable filestream for new Database</p></div>
<p>Or you can do this via TSQL like this</p>
<pre class="brush: sql;">
CREATE DATABASE FileStreamDemo
ON PRIMARY
   (NAME = FileStreamDemo,
      FILENAME = N'D:\DB\FileStreamDemo_data.mdf'),
FILEGROUP FileStreamFileGroup CONTAINS FILESTREAM
   (NAME = FileStreamDemo,
      FILENAME = N'D:\DB\FileStreamDemo')
LOG ON
   (NAME = 'FileStreamDemo_log',
      FILENAME = N'D:\DB\FileStreamDemo_log.ldf');
go
</pre>
<p>After doing this, SQL Server will create Folder in &#8220;D&#8221; drive, with name FileStreamDemo under DB directory. This FileStreamDemo folder will contains two files</p>
<ol>
<li>filestream.hdr &#8211; This is the FILESTREAM metadata for the data container.</li>
<li>The directory $FSLOG. This is the FILESTREAM equivalent of a database’s transaction log.</li>
</ol>
<p><strong>Creating a Table with FILESTREAM Data</strong><br />
You can create a Table for consuming FileStream like this.</p>
<pre class="brush: sql;">
CREATE TABLE SQLFileSystem
(
FileId UNIQUEIDENTIFIER  ROWGUIDCOL UNIQUE DEFAULT NEWID() PRIMARY KEY,
FileName VARCHAR(255),
FileContents VARBINARY(MAX) FILESTREAM  NULL default (0x)
)
</pre>
<p>Thats it, you have created SQL Server Database and Table with Filestream.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/07/06/filestream-in-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
