<?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>dotnet thoughts &#187; SQL Server 2008</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/sql-server-2008/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>a dotnet developer&#039;s technical blog</description>
	<lastBuildDate>Wed, 08 Feb 2012 03:18:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to enable remote connections to SQL Server 2008 using command line</title>
		<link>http://www.dotnetthoughts.net/2011/10/05/how-to-enable-remote-connections-to-sql-server-2008-using-command-line/</link>
		<comments>http://www.dotnetthoughts.net/2011/10/05/how-to-enable-remote-connections-to-sql-server-2008-using-command-line/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 12:38:55 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Commandline]]></category>
		<category><![CDATA[Remote Connections]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL2008]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=1838</guid>
		<description><![CDATA[Here is a small sql snippet, which will helps to enable remote connections using Command Line. It will enable remote connections to your SQL Server. Happy Coding No related content found.]]></description>
			<content:encoded><![CDATA[<p>Here is a small sql snippet, which will helps to enable remote connections using Command Line.</p>
<pre class="brush: sql; title: ; notranslate">
EXEC sys.sp_configure N'remote access', N'1'
GO
RECONFIGURE WITH OVERRIDE
GO
</pre>
<p>It will enable remote connections to your SQL Server.<br />
Happy Coding <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="betterrelated none"><p>No related content found.</p></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2011/10/05/how-to-enable-remote-connections-to-sql-server-2008-using-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding and Reading files from SQL Server 2008 Filestream</title>
		<link>http://www.dotnetthoughts.net/2009/09/22/adding-and-reading-files-from-sql-server-2008-filestream/</link>
		<comments>http://www.dotnetthoughts.net/2009/09/22/adding-and-reading-files-from-sql-server-2008-filestream/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 12:38:46 +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[SQL Server 2008]]></category>
		<category><![CDATA[VB.Net]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=393</guid>
		<description><![CDATA[Few days back I wrote a Post about FileStream feature in SQL 2008.(Filestream in SQL Server 2008). In this post I am trying to write the how to manage or use the FileStream feature from .Net managed code. For adding &#8230; <a href="http://www.dotnetthoughts.net/2009/09/22/adding-and-reading-files-from-sql-server-2008-filestream/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Few days back I wrote a Post about FileStream feature in SQL 2008.(<a href="http://www.dotnetthoughts.net/2009/07/06/filestream-in-sql-server-2008/">Filestream in SQL Server 2008</a>). In this post I am trying to write the how to manage or use the FileStream feature from .Net managed code.</p>
<p>For adding a File to FileStream enabled table, we are using a new Data Type, SqlFileStream, which comes with .Net 3.5.</p>
<p>I am using the Table structure as in the Previous post, as the example, that Table contains 3 columns, FileID(PK), FileName and FileContents.<br />
For inserting a File you need to begin a transaction, insert an row in to the Table, without the contents of the File, for reading the Transaction context. Then using FileStream, write the contents of the File to the Row. After writting the stream, need to commit the transaction to update the file to the Database. I am writing the code in VB.Net.</p>
<p>Code for saving the File to the Database</p>
<pre class="brush: vb; title: ; notranslate">
Private Sub AddFile(ByVal fileName As String)
	Dim _Transaction As SqlTransaction
	Dim _Command As SqlCommand
	Dim _DataReader As SqlDataReader
	Dim _Connection As SqlConnection
	Dim _FileId As String
	Dim _FilePath As String
	Dim _FileNameParameter As SqlParameter
	Dim _FileIdParameter As SqlParameter
	Dim _FilePathParameter As SqlParameter

	Dim _SQLFileStream As SqlTypes.SqlFileStream
	Dim _Context As Byte()

	Try
		_Connection = New SqlConnection(&quot;Server=.\SQL2008;Integrated Authentication=SSPI;Database=FileSystemExample;&quot;)
		_Connection.Open()
		_Transaction = _Connection.BeginTransaction()
		_Command = New SqlCommand(&quot;xsp_InsertFile&quot;, _Connection, _Transaction)
		_Command.CommandType = CommandType.StoredProcedure
		_Command.Parameters.AddRange(New Object() {_FileIdParameter, _FileNameParameter, _FilePath})
		_FileId = Guid.NewGuid().ToString

		_FileIdParameter = New SqlParameter(&quot;@FileId&quot;, SqlDbType.UniqueIdentifier)
		_FileIdParameter.Value = _FileId

		_FileNameParameter = New SqlParameter(&quot;@FileName&quot;, SqlDbType.VarChar)
		_FileNameParameter.Value = fileName

		_FilePathParameter = New SqlParameter(&quot;@FilePath&quot;, SqlDbType.VarChar)
		_FilePathParameter.Direction = ParameterDirection.Output

		_DataReader = _Command.ExecuteReader(CommandBehavior.SingleRow)
		If _DataReader.HasRows Then
			_FilePath = _DataReader(&quot;FilePath&quot;).ToString
		End If
		If Not _DataReader.IsClosed Then
			_DataReader.Close()
		End If

		_Command = New SqlCommand(&quot;SELECT GET_FILESTREAM_TRANSACTION_CONTEXT() FROM SQLFileSystem&quot;, _Connection, _Transaction)
		_Context = _Command.ExecuteScalar()

		_SQLFileStream = New SqlFileStream(_FilePath, _Context, FileAccess.Write)
		_SQLFileStream.Write(_Context, 0, _Context.Length)
		_SQLFileStream.Close()

		_Transaction.Commit()
	Catch ex As Exception
		If _Transaction IsNot Nothing Then
			_Transaction.Rollback()
		End If
	Finally
		If _Connection IsNot Nothing Then
			_Connection.Close()
		End If
	End Try
End Sub
</pre>
<p>Code for reading the File from the Database</p>
<pre class="brush: vb; title: ; notranslate">
Private Function ReadFile(ByVal fileId As String) As Byte()
	Dim _Transaction As SqlTransaction
	Dim _Command As SqlCommand
	Dim _DataReader As SqlDataReader
	Dim _Connection As SqlConnection
	Dim _FilePath As String
	Dim _FileIdParameter As SqlParameter

	Dim _SQLFileStream As SqlTypes.SqlFileStream
	Dim _Context As Byte()

	Try
		_Connection = New SqlConnection(&quot;Server=.\SQL2008;Integrated Authentication=SSPI;Database=FileSystemExample;&quot;)
		_Connection.Open()
		_Transaction = _Connection.BeginTransaction()
		_Command = New SqlCommand(&quot;SELECT FileStreamData.PathName() AS [FilePath],GET_FILESTREAM_TRANSACTION_CONTEXT() AS [Context] FROM SQLFileSystem WHERE FileId=@FileId&quot;, _Connection, _Transaction)
		_Command.CommandType = CommandType.StoredProcedure

		_FileIdParameter = New SqlParameter(&quot;@FileId&quot;, SqlDbType.UniqueIdentifier)
		_FileIdParameter.Value = fileId
		_Command.Parameters.Add(_FileIdParameter)

		_DataReader = _Command.ExecuteReader(CommandBehavior.SingleRow)
		If _DataReader.HasRows Then
			_FilePath = _DataReader(&quot;FilePath&quot;).ToString()
			_Context = TryCast(_DataReader(&quot;Context&quot;), Byte())
		End If

		_SQLFileStream = New SqlFileStream(_FilePath, _Context, FileAccess.Read)
		_SQLFileStream.Read(_Context, 0, _Context.Length)
		_SQLFileStream.Close()

		_Transaction.Commit()
	Catch ex As Exception
		If _Transaction IsNot Nothing Then
			_Transaction.Rollback()
		End If
	Finally
		If _Connection IsNot Nothing Then
			_Connection.Close()
		End If
	End Try
	Return _Context
End Function
</pre>
<p>Stored Procedure</p>
<pre class="brush: sql; title: ; notranslate">
CREATE PROCEDURE xsp_InsertFile
(
@FileName VARCHAR(255),
@FileId UNIQUEIDENTIFIER,
@FilePath VARCHAR(MAX) OUTPUT)
AS
BEGIN
SET NOCOUNT OFF
INSERT INTO SQLFileSystem (FileId, FileName) VALUES(@FileId, @FileName)
SELECT @FilePath = SystemFile.PathName() from SQLFileSystem where FileId = @FileId
END
</pre>
<p>Note: I didn&#8217;t tested the code. Please let me know if you found any issues in the implementation.</p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2009/10/07/how-to-store-and-retrieve-files-from-sql-server-database/" title="Permanent link to How to Store and Retrieve files from SQL Server Database">How to Store and Retrieve files from SQL Server Database</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2008/05/08/using-iocompression-namespace/" title="Permanent link to Using IO.Compression namespace">Using IO.Compression namespace</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/07/06/filestream-in-sql-server-2008/" title="Permanent link to FILESTREAM in SQL Server 2008">FILESTREAM in SQL Server 2008</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/22/implementing-custom-paging-in-datarepeater-using-c-and-sql-server/" title="Permanent link to Implementing Custom Paging in DataRepeater using C# and SQL Server">Implementing Custom Paging in DataRepeater using C# and SQL Server</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/20/treeview-population-without-recursive-function/" title="Permanent link to TreeView Population without recursive function">TreeView Population without recursive function</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/09/22/adding-and-reading-files-from-sql-server-2008-filestream/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 &#8230; <a href="http://www.dotnetthoughts.net/2009/07/06/filestream-in-sql-server-2008/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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; title: ; notranslate">
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; title: ; notranslate">
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; title: ; notranslate">
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>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2009/09/22/adding-and-reading-files-from-sql-server-2008-filestream/" title="Permanent link to Adding and Reading files from SQL Server 2008 Filestream">Adding and Reading files from SQL Server 2008 Filestream</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/07/how-to-store-and-retrieve-files-from-sql-server-database/" title="Permanent link to How to Store and Retrieve files from SQL Server Database">How to Store and Retrieve files from SQL Server Database</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/07/30/implementing-windows-authentication-in-asp-net-on-iis-7/" title="Permanent link to Implementing Windows authentication in ASP.NET on IIS 7">Implementing Windows authentication in ASP.NET on IIS 7</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/20/treeview-population-without-recursive-function/" title="Permanent link to TreeView Population without recursive function">TreeView Population without recursive function</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/07/04/drag-and-drop-files-from-windows-to-your-application/" title="Permanent link to Drag and Drop files from Windows to your application">Drag and Drop files from Windows to your application</a>  </li>
</ol></div>]]></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>

