Reading binary data from SQL Server and then De-Serializing it

For one of our applications we needed to serialize Excel 2003 files as XML documents in the database. And then as and when required by the application we were to de-serialize them. Following is the code for de-serializing xml file. One side note for developers, whenever you want to read blob or text or binary data that is massive in size then always make sure that you use propper command behaviour(System.Data.CommandBehaviour.SequentialAccess)for your ExecuteReader method of SqlCommand. Since by default ExecuteReader method loads all the row and provides you (named/indexed) collection of records. But there is one catch with this and that is: If you have opted for sequential access then you will have to be watchful about the sequence in which you will access your data. i.e. for example if you gave read nth column from reader then you can only proceed to read columns greater (>) n and can never read any column less than or equal to (<= ) n

      if(l_rdrSqlReader.GetValue(1)!= DBNull.Value)

       {

        /// READ STREAM

        l_buffer    =     (byte[]) l_rdrSqlReader[1];

      }

      else

      {

         l_buffer    = null;

      }

l_buffer is a byte array to hold contents. 

                        if(l_buffer!= null && l_buffer.Length    >0)                        {                                   // if file is alread there then delete it                              if(System.IO.File.Exists(l_strfileName+".xls"))                              {                                  System.IO.File.Delete(l_strfileName+".xls");                          

                              }

                              /// create file stream to write data on to

                              l_fsExcel   =     new System.IO.FileStream(l_strfileName+".xls",System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.Write);

                              /// create binary writer from the byte array buffer

                              l_txtWriter =     new System.IO.TextWriter(l_fsExcel);                     

                              /// write the buffer onto stream

                              l_txtWriter.Write(l_buffer,0,l_buffer.Length);

                              /// flush the output and close.

                              l_binWriter.Flush;

                              l_binWriter.Close;

                        }


Hammad Rajjoub, MVP (Windows Server System - XML Web Services),

User Group Leader - Dot Net Wizards (http://dotnetwizards.blogspot.com), Chariman UG Relations Committee , Member Speakers Bureau

This article is part of the GWB Archives. Original Author: INETA Pakistan

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.