1

I have a console application using C# where I want to read the blob stored in SQL Server and write it to windows file system at a specified path.

How do I convert a blob to a image of pdf using C# and write it to a file system?

Juliet
  • 80,494
  • 45
  • 196
  • 228
Anuya
  • 8,082
  • 49
  • 137
  • 222
  • @oded, I have no idea how to do it...never worked with blob... – Anuya Apr 28 '11 at 19:13
  • possible duplicate of [Retrieving image from sql database](http://stackoverflow.com/questions/1697194/retrieving-image-from-sql-database) – Pharabus Apr 28 '11 at 19:13
  • Answers to this question for SQLite might help you: http://stackoverflow.com/questions/625029/how-do-i-store-and-retrieve-a-blob-from-sqlite/625485#625485 Also, entering "sql server c# blob read" in to Google returned loads of pages of info. – Tony Apr 28 '11 at 19:14
  • And here is another good question/asnwer from SO if you are dealing with large blobs: http://stackoverflow.com/questions/1487808/memory-effective-way-to-read-blob-data-in-c-sql-2005 – Tony Apr 28 '11 at 19:17

3 Answers3

1

Read the blob from the database into a byte[] and write this buffer to a file.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • blob can be of any file type is it ? how to the program recognize that its an image file or pdf file ? – Anuya Apr 28 '11 at 19:16
  • 1
    @Anuya - it can't and it doesn't. You need to tell it. In most cases, when saving a blob to a database, some additional data is saved with it to identify the type (unless all files are of the same type). – Oded Apr 28 '11 at 19:18
1

Take a look here: http://www.akadia.com/services/dotnet_read_write_blob.html

Using a DataAdapter / DataSet would most likely prove even easier - if you can afford to have the entire BLOB content loaded into memory for the number of rows you're processing.

Will A
  • 24,780
  • 5
  • 50
  • 61
0

What kind of blob are we talking about here? Assuming you are using .NET, Check out SqlDataReader or Entity Framework. There are other methods as well but those 2 are pretty popular. Converting to PDF you will probably need a 3rd party tool, check out PDF Sharp. Finally, saving to filesystem is pretty straightforward, look at the .NET System.IO.File class

donnovan9
  • 150
  • 2
  • 9