SQL 2008 FILESTREAM

July 24, 2010 05:25 by wjchristenson2

The FILESTREAM feature of SQL 2008 allows more effective storage and retrieval to BLOB data.  It accomplishes this by utilizing both SQL 2008 and the NTFS file system.  In the past, most developers stored BLOB data in the SQL Server using varbinary(max).  This has some drawbacks including:  you had a 2GB max, streaming performance was awful, and recovery from fragmentation was poor.  The FILESTREAM can hold up to the size of the NTFS volume size, streaming performance is excellent, and it can recover from fragmentation on disk nicely.

The one drawback that I can see is when the client needs to perform small & frequent BLOB updates.  Rather than writing to SQL Server data files, FILESTREAM writes to the file system.  Locating, creating, updating, deleting files outside of the SQL Server data files do come at a cost.  So some consideration as to the size & frequency of BLOB data would need to be made.

There are a few ways to enable FILESTREAM (SQL Server Configuration Manager, SQL Server Management Studio properties of the connected instance, and via TSQL scripting).  Personally I was only able to get it to work by using the SQL Server Configuration Manager.  Apparently installing Visual Studio/SQL 2005 can screw up FILESTREAM features of SQL 2008…  Anyway, here’s the basic FILESTREAM access levels which you’ll need to understand when enabling it.

FILESTREAM access levels:
a) 0 = Disables FILESTREAM support for this instance.
b) 1 = Enables FILESTREAM for Transact-SQL access.
c) 2 = Enables FILESTREAM for Transact-SQL and Win32 streaming access.

I created a script which enables FILESTREAM for the SQL Instance, then it enables it for my “testdb” database.  It auto-detects where your database’s data/log files reside and then creates a directory in there for FILESTREAM objects.

 

USE [master];
GO
DECLARE @filestream_access_level SMALLINT;
SELECT @filestream_access_level = CONVERT(SMALLINT, SERVERPROPERTY('FilestreamEffectiveLevel'));

IF @filestream_access_level <> 2
BEGIN
	EXEC sp_configure filestream_access_level, 2;
	RECONFIGURE WITH OVERRIDE;
END;
GO

USE [testdb];
GO
IF NOT EXISTS (SELECT * FROM sys.filegroups AS fg WITH (NOLOCK) WHERE name = 'testdb_FILESTREAM')
BEGIN
	--add a file group and indicate that it will contain FILESTREAM objects
	ALTER DATABASE testdb ADD FILEGROUP testdb_FILESTREAM CONTAINS FILESTREAM;
END;
GO
IF NOT EXISTS (SELECT *
	FROM sys.filegroups AS fg WITH (NOLOCK)
		INNER JOIN sys.database_files AS df WITH (NOLOCK) ON fg.data_space_id = df.data_space_id
	WHERE fg.name = 'testdb_FILESTREAM'
		AND df.name = 'testdb_FILESTREAM')
BEGIN
	DECLARE @path NVARCHAR(MAX), @sql_statement NVARCHAR(MAX);
	SELECT @path = mf.physical_name FROM sys.master_files AS mf WITH (NOLOCK) WHERE mf.name = 'testdb' AND mf.type_desc = 'ROWS';
	SELECT @path = SUBSTRING(@path, 1, LEN(@path) - CHARINDEX('\', REVERSE(@path))) + '\testdb_FILESTREAM';

	--tell the file group where the FILESTREAM objects should be stored  
	SELECT @sql_statement = N'ALTER DATABASE testdb ADD FILE (NAME = ''testdb_FILESTREAM'', FILENAME = ''' + @path + ''') TO FILEGROUP testdb_FILESTREAM;';

	EXEC sp_executesql @statement = @sql_statement;
END;
GO

 

At this point I was ready to design a table in SQL Management Studio with a FILESTREAM column.  A FILESTREAM column is actually a varbinary(max) column with the FILESTREAM attribute.  Well, the table designer in SQL Management Studio does not support specifying the FILESTREAM attribute.  You have to script it.

Here’s some rules which you’ll need to adhere to when creating FILESTREAM columns.
1) The table can have multiple FILESTREAM columns, but they must all be in the same FILEGROUP.
2) If you don’t specify the FILESTREAM_ON clause, the default FILEGROUP will be used.
3) The table must have a uniqueidentifier column with the ROWGUIDCOL attribute.  It cannot contain NULL values and must have either a UNIQUE or PRIMARY KEY single column constraint.

 

USE [testdb];
GO 
IF NOT EXISTS (SELECT * FROM sys.tables AS t WITH (NOLOCK) WHERE t.name = 'Document' AND t.type = 'U')
BEGIN
	CREATE TABLE dbo.Document (
		DocumentID INT IDENTITY PRIMARY KEY,
		DocumentGUID UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL UNIQUE DEFAULT NEWID(),
		Document VARBINARY(MAX) FILESTREAM NULL
	) ON [PRIMARY];
END;
GO

 

In my next post, I will show .NET streaming capabilities with the new FILESTREAM features in SQL 2008.
 

Bookmark and Share

WCF DateTime Serialization

May 11, 2010 02:24 by wjchristenson2

By default, if the programmer does not specify the DateTime kind, .NET WCF services will serialize and deserialize DateTime objects based on the local time zone.  What this means is that if you have a client entering information on the east coast (EST) and your server is in central time (CST), the DateTime values entered by the client will have their hour decremented by 1 when the data is deserialized at the server.

The reason why WCF behaves this way is because it thinks that the DateTime entered on the client is in local time.  As a result, when the value reaches the server, 11:00 AM client local time is converted to 10:00 AM because that is what the local time is on the server.

If this is not what is desired, specifying the kind of the DateTime value appears to alter the serialization/deserialization behavior.  Specifying what kind of date your value is will not alter the underlying value.

Unspecified - No conversion:  returnValue = DateTime.SpecifyKind(value, DateTimeKind.Unspecified)
Local - Time Zone conversions:  returnValue = DateTime.SpecifyKind(value, DateTimeKind.Local) 
UTC – No conversions:  returnValue = DateTime.SpecifyKind(value, DateTimeKind.Utc)

Bookmark and Share

PowerCommands for Visual Studio

May 7, 2010 03:09 by wjchristenson2

PowerCommands is a set of extensions for Visual Studio which adds functionality to the IDE.  It is the #1 downloaded/rated plug-in for Visual Studio 2008 and 2010.  So I felt a bit silly as I had never heard of it until a few days ago.  Some things always bugged me about Visual Studio (ie: having to collapse each project in a solution one at a time, not being able to close all open documents at once, not being able to open the containing folder of a document from the document window, etc).  PowerCommands has these features and many more.

I’d encourage developers who use Visual Studio regularly to check it out.  It’s free and easy to install.  Below are the links to the 2 versions of PowerCommands:

PowerCommands for Visual Studio 2008
PowerCommands for Visual Studio 2010

Bookmark and Share

Silverlight 4 Final – Released 4/15/2010

April 18, 2010 05:59 by wjchristenson2

On Thursday 4/15/2010, Microsoft officially released the final version Silverlight 4.  Scott Guthrie gave a 60 minute keynote on Silverlight 4 a few days prior.  If you haven’t watched the keynote, I’d recommend doing so.  I was impressed with the new features and the demos provided.  Silverlight 4 includes a ton of new features. 

Here is a quick list of some of the new features that I found interesting:

  • Tooling – Multi-Targeting Silverlight 3 and 4.  You can also now design within VS 2010.
  • Printing API – You can now print from Silverlight.
  • Right-Click Event Handling – MouseRightButtonUp/Down events are now available.
  • Webcam & Microphone Access – SL can now use the client’s webcam(s) and microphone(s).
  • Mouse Wheel Support – The mouse wheel event was added in SL 3, but now TextBox, ComboBox, Calendar, DatePicker, and the ScrollViewer auto-scroll.  You no longer have to manually handle the event.
  • RichTextArea Control – Provides an area where users can create and edit text and specify bold/italic/underlined/etc text.
  • Google Chrome Support - M$ finally now supports Google Chrome.
  • Implicit Theming – Create a style for a targeted type and all of those types will implicitly use it.
  • Fluid UI Support in the ItemsControl – 3 new states: BeforeLoaded, Loaded, and Unloaded.
  • DataGrid Enhancements – Column relative width sizing refactored.
  • DataBinding Enhancements – Binding can now use StringFormat, TargetNullValue, FallbackValue (e.g. Dates no longer requires ValueConverters to format the date).
  • IDataErrorInfo – When this interface is implemented, it reports data validation errors that a UI can bind to.  Only one property is validated/reported on at a time.
  • INotifyDataErrorInfo – Allows developers to provide custom validation logic server-side via asynchronous routines.  Here is a video on the new data validation features.
  • Silverlight Drop Target – Drag and drop folders/files into your Silverlight application.
  • ViewBox – New control which is used to simplify the resizing of images.
  • Text Trimming – Auto adding of word ellipse (…)
  • Keyboard Access in Full Screen Mode – If SL is in full screen mode – all keyboard input is accepted in Silverlight as long as the application is trusted.
  • Network Authentication – You can now pass separate credentials when connecting with 3rd party service providers.
  • COM Interop – Silverlight can now create COM objects – however this only applies to trusted SL applications.
  • Notification (“Toast”) API – Notifications (like a new Outlook email as arrived) can now be used.
  • Local File Access – SL can now access the user’s “My” folders (e.g. My Documents, etc).  This requires trusted SL application.
  • Elevated Trust Applications – Many new features in SL4 require elevated trust.  This new feature will prompt the user to allow/fully trust the SL application when installed.
  • HTML Hosting with WebBrowser – You can now show and host HTML within your SL application.
  • Clipboard API – SL now has access to copy to and paste from the clipboard.
Bookmark and Share

ASP.NET MVC First Impressions

February 21, 2010 14:06 by wjchristenson2

Today I decided to make a small ASP.NET MVC application which would add, edit, delete, and list US states which resided in a SQL Server Express database.  I am a veteran in ASP.NET Web Forms development, so I wanted to see what MVC is like.  I create business objects/models everyday, so understanding models was easy enough.  The tricky part for me was creating and understanding how controllers and views interact with each other.

First Impressions:
1)  I liked the clean separation between controller/view/models.  Basically this is required when using MVC.  I’ve already been accustomed to separating layers, so this wasn’t new to me, but seeing how the MVC Framework has this built in was refreshing.
2)  When I created a new MVC Web Application, it automatically prompted to create a Test project.  I thought that was pretty neat.  I’m not experienced in creating automated test yet, but I’m sure I’ll get there soon and just seeing the parallelism with MVC and testing was impressive.
3)  REST (Representational State Transfer) URLs was interesting.  Apparently you can configure how requests are mapped to controllers in the Global.asax.  I didn’t get into this much as I took the defaults, but simply editing a state looked like such: “/states/Edit/1”.  No more mapping to a template/resource.
4)  You can generate a view straight from the controller by right-clicking when you return an ActionResult.  Visual Studio will then prompt/ask you what kind of view you want to make and you can also have it create a strongly typed view so that you can access the model’s properties.  So the framework can assist in starting a view pretty quickly for you.
5)  Not having view state and post backs was really nice.  The rendered HTML was very clean.
6)  I was confused at times as to how the framework knew what to pass to controller routines on a form post and also how id’s work.  What if you have more than 1 property that makes up the id?  I imagine this would come with time.

Overall I enjoyed the initial experience and it seemed very structured.  I feel that one day doesn’t do it justice as MVC is a bit more complex than web forms.

Helpful Resources:
1)  Scott Guthrie’s NerdDinner.com Chapter:  Scott Guthrie wrote a chapter in Professional ASP.NET MVC 1.0 in which he walks you through step-by-step in creating an MVC web application.  You can download the chapter in PDF and also download the source code if needed.
2)  Simple E-Commerce Storefront Application: This is another Scott Guthrie example, however this example has some nice architecture diagrams that go along with it to explain what’s going on a bit more.

MVCTest_Soln.zip (891.39 kb)

ASP.NET MVC Overview Part 1 (General ASP.NET MVC Overview)
ASP.NET MVC Overview Part 2 (Advantages of MVC and Web Forms)
ASP.NET MVC First Impressions

Bookmark and Share