Archive for the 'MSDN' Category

06th Mar 2008

Microsoft Cloud Storage Is Finally Here

imageFor those who have been following Microsoft’s S+S message you may be excited to hear about SQL Server Data Services (SSDS) which was announced at the MIX08 Keynote today.

So what is this new cloud storage offering by Microsoft anyway?

  • Microsoft hosted data storage service based on SQL Server
  • Accessible via REST and SOAP web services APIs

It’s an exciting addition to the Microsoft line up of data storage products and services.  It’s great to finally be able to talk about it and we are really excited to see how developers use the service.

I’ll continue to blog about this as well as post some samples.

Posted in Cloud Storage, MSDN, SQL Server, SSDS | No Comments »

03rd Mar 2008

To FILESTREAM or not to FILESTREAM… That is the question.

Roger and I have been delivering a demo which highlights Spatial and FILESTREAM features of SQL Server 2008.  One of the common things we’ve heard from developers is…

For years we’ve been told that large binary files should never be stored in the database…  Are you telling us to start storing these files in the database now?  If so, why?  Are we just supposed to throw this best practice out the window?

This is really a great question which has prompted some interesting discussions.  Obviously the answer is not black and white. So let’s start by looking at what has changed.

 

SQL Server 2008 is now a very powerful engine for storing binary files. 

  • These files can be accessed through high performance Win32 streaming API’s in addition to T-SQL.
  • These files are managed by SQL Server in their own file groups which can be backed up restored along with the rest of your SQL Server data.  On the flip side you aren’t required to backup and restore these file groups.
  • Reading and writing these files can now be part of a database transaction.

So you might be thinking to yourself…

Sounds great!!!  Let’s start storing all of our binary data in SQL Server.

Well, there are some considerations to be made before signing up to rewrite your app to take advantage of FILESTREAM.  Here are some of the main considerations.

Do other applications need direct access to your binary files?

If you read my article about writing files to FILESTREAM you probably noticed that you have to go through SQL Server to access the data in FILESTREAM.  There is no concept of
OpenFile(”C:\Path_To_My_File\File_Name.docx”)

Does your architecture require database mirroring?

Database mirroring does not yet support FILESTREAM.

Those are just a couple of the things to think about.  I’d recommend checking out our FILESTREAM sample on CodePlex and make some decisions for yourself. 

FILESTREAM is a great technology and we are really excited to see how developers incorporate it into their applications.  Feel free to post comments here about your experience integrating FILESTREAM into your architecture.

Posted in FileStream, MSDN, SQL Server | No Comments »

03rd Mar 2008

SQL Server 2008 FILESTREAM and WPF MediaElement - Part 2 (Writing FILESTREAM Data)

codeplex-logo Wow…  It’s been a long time coming.  I promised that I would explain in more detail how to write FILESTREAM data to SQL Server 2008.  This is the second article in a series and uses the sample published on the SQL Server Community Samples site on CodePlex.

Writing data to a varbinary(max) FILESTREAM column in SQL is a bit more involved then just opening a file on the filesystem.  SQL Server needs to manage this operation within a transaction which adds a bit of complexity.  Here are the basic steps…  These steps apply both to reading and writing.

  1. Start a SQL Server transaction
  2. Insert a row into the table containing metadata
  3. Select the PathName from SQL Server which will be used to get a handle
  4. Open a handle for writing using sqlncli10.dll
  5. Use that handle within System.IO classes
  6. Commit the transaction

Now that the basic steps are laid out, let’s take a closer look.

// Start up a database transaction.
SqlTransaction txn = cxn.BeginTransaction();

No need for explanation there.

// Insert a row into the table to create a handle for streaming write.
SqlCommand cmd = new SqlCommand(“INSERT [dbo].[media]([mediaId], [fileName],         [contentType]) VALUES( @mediaId, @fileName, @contentType);”, cxn, txn);

This is worth a bit talking about.  Why do you need to insert a row with metadata?  The answer is that in order to get a handle to the FILESTREAM column the row cannot have a NULL value in the FILESTREAM column.  This took some trial and error to discover. 

If you look closely at the create table script in the sample code you will see that the varbinary(max) FILESTREAM column default is set to a zero byte binary value.

file varbinary(max) FILESTREAM DEFAULT(0x)

This should make a bit more sense once we look at the next step.

// Get a filestream PathName token and filestream transaction context.
// These items will be used to open up a file handle against the empty blob instance.
cmd = new SqlCommand(“SELECT [file].PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT()         FROM [dbo].[media] WHERE [mediaId] = @mediaId;”, cxn, txn);
...
// Read in results of file.PathName()
SqlString sqlFilePath = rdr.GetSqlString(0);

So…  Here’s where I think things get interesting.  You can see that the SELECT statement above calls the PathName() method of the FILESTREAM column.  Here’s an example of the results from the query above. Note the use of UDTs and CLR!

\\ZOWENS-NB3\MSSQLSERVER\v1\FilestreamWpfHttp\dbo\media\file\4C3C9C2D-8268-43FF-8317-D507319FE21C

This is a “virtual” path managed by SQL Server.  It consists of \\COMPUTER_NAME, followed by a configurable handler \MSSQLSERVER…

Now what?

// Get a Win32 file handle to the empty blob instance using SQL Native Client call.
// This is required in order to write to the empty blob instance.
SafeFileHandle handle = SqlNativeClient.OpenSqlFilestream(
        sqlFilePath.Value,
        SqlNativeClient.DESIRED_ACCESS_WRITE,
        0,
        transactionToken.Value,
        (UInt32)transactionToken.Value.Length,
        new SqlNativeClient.LARGE_INTEGER_SQL(0));

The code above uses a simple C# class “SqlNativeClient” that wraps the sqlncli10.dll I mentioned above.  This C# class is key to working with FILESTREAM in managed code.  You can see that we passed in the sqlFilePath variable from the PathName() query.

// Open up a new stream to write the file to the blob.
System.IO.FileStream destBlob = new System.IO.FileStream(handle, FileAccess.Write);

“Old school” System.IO file manipulation using the handle obtained from the SqlNativeClient class above.

// Commit transaction
txn.Commit();

There you have it…  It’s not rocket science but there are a few tricks.

Posted in FileStream, MSDN, SQL Server | No Comments »

20th Feb 2008

Feature Complete CTP

As of yesterday the SQL Server 2008 February CTP is available for download here.

This is a really exciting release as it’s feature complete and is localized.  Take a look and make sure to provide feedback through MSDN and TechNet.

Posted in MSDN, SQL Server | No Comments »

18th Dec 2007

FILESTREAM Support in SQL Server Express

A couple great questions came up last week when discussing FILESTREAM with some people within the Microsoft field which I thought were worth sharing.

 

Q: Will SQL Server 2008 Express edition support FILESTREAM?

A: It sure will!

 

Q: Will the 4GB limit of Express apply to data stored in the FILESTREAM?

A: Nope…  Go ahead and store large binary files!

 

As you may have noticed I’m really excited about binary data becoming a first class citizen in SQL Server 2008.  Once you dig into it I think you will agree.

Posted in FileStream, MSDN, SQL Server | No Comments »