Easy SQL Script for handling SQL Image Data

Obviously, I’m not a SQL expert and I was focusing on WPF, Silverlight and patterns during last two years. But recently, I got an opportunity to wear multiple hats in my new job and SQL is the one of the hats that I’m wearing now. :)

Okay. Let’s talk about this post.. This post is about how to read the SQL image data, save it as an image file and how to update the existing image data with new image file from SQL Query Panel.

This is the screenshot of one of our games. As you can see in this screenshot, there are some images that we are using in our Silverlight game. Those image comes from database.

I thought that SQL 2008 R2 Management Studio would have something that allows us to view or change the existing images (binary) but Unfortunately, it has nothing that you can view the images. (Please take a look at the screenshot below. This is how you will see the image data in SQL 2008 R2 Management Studio. very powerful tool, isn’t it? :) )

I looked for open source or free tool that I can view the image from SQL database and save it as a file. I found a few trail tools but none of them are good enough. I decided to do some experiments on SQL script and here is something that I come up with.. I’m not sure whether this is a good solution for you guys or not but it’s good solution that I can live with for now. If you have any good solution that you are using in your work then please feel free to let me know..

Let’s take a look at the script.

Enabling Ole Automation Procedures in SQL

Before running the script, you need to enable “Ole Automation Procedures” by using the script below. Note that you need to have an administrative right to run the script.

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO

Once you enable the option then you should be able to read the image by using Ole and save it as a file.

Reading Image Data and Save it as a image file

You need to set the path where you want the generated image to be saved. Did you notice that I mentioned the file extension in @filePath? It’s because I know that all images that we are using are .jpg but it would be great if we can write something that can detect the image format from the binary data. If you have the script please share with me. ^^

DECLARE @objStream INT
DECLARE @imageBinary VARBINARY(MAX)
SET @imageBinary = (SELECT imagedata FROM dbo.tblDefaultImages WHERE imageid = 121)
DECLARE @filePath VARCHAR(8000)
SET @filePath = 'C:\\MS\\r.jpg'
EXEC sp_OACreate 'ADODB.Stream', @objStream OUTPUT
EXEC sp_OASetProperty @objStream, 'Type', 1
EXEC sp_OAMethod @objStream, 'Open'
EXEC sp_OAMethod @objStream, 'Write', NULL, @imageBinary
EXEC sp_OAMethod @objStream, 'SaveToFile', NULL,@filePath, 2
EXEC sp_OAMethod @objStream, 'Close'
EXEC sp_OADestroy @objStream

Updating the image

Here is the script for updating the existing binary data with new image file. I didn’t use Ole here because OpenRowSet is very easy to use and much shorter..

UPDATE [YourDatabase].[dbo].[tblDefaultImages]
SET imagedata =  (SELECT * FROM
OPENROWSET(BULK N'E:\\FTPRoot\\MS\\rhyme\\rhymeObjects\\wine.png', SINGLE_BLOB)
AS Document)
WHERE imageid = 127

Okay. That’s.. What do you think?

Silverlight 4 – WCF RIA Services Presentation

Just in case if you are not following our Singapore Silverlight User Group’s website, here is the WCF RAI presentation slide from our last meetup. Please feel free to download it.. I hope it would be helpful if you are also preparing a talk about WCF RIA.. :)

Download ~

Related ~

Updates

Good morning! I hope you guys are doing well. I’m doing all right. I’ve been so busy lately and was not able to update anything on my blog for long time. I’m very sorry about that. The problem with me is that I want to know or experiment a lot of things so I keep on taking more tasks even I already have a lot of things in my hand. At the end, I always end up working late or working on weekends. I know this is definitely not a good way of living.. I will have to manage my time carefully and need to put more efforts on planning.

Okay. Let me update you guys on what I’m doing lately.

Changes in my blog

I managed to spent some times for changing a few things in my blogs today. I wanted to make those things since long time back but it took quite a while to finish the whole things so I ended up rolling back my changes after I’ve done here and there. Finally, I finished changing almost everything that I want for my blog. I’m very happy about it. :)

1. New Theme

I’ve been using the WordPress theme called “Fluid Solution 1.0″ created by Viktor Persson in my blog for long time. I forgot to blog when I changed that theme so I don’t remember how long my blog has been wearing it. (BTW, if you are interested in viewing my old blog themes, you can check them here,  here and here. ) I’m glad that I finally found new awesome theme created Shlomi Noach in wordpress theme directory and I’m going to start using it for my blog.

2. Feedburner

I accidentally removed my feedburner image when I switched my theme from Freshy to Fluid Solution. I got some feedbacks from my reader saying that they used to have problem in finding my RSS feed so I re-added my feedburner image that I used to have earlier. Now, you can click on Feedburner image to subscribe my feed. Or, please click here you can also subscribe me by email address.

I added one more widget that you can subscribe my feed by email address in right-bar…

3. Social network sharing

I added three plugins that can help you for sharing your favorite posts in your social network or with your friends. For me, I used to use the browser plugins or copy the URL manually to share something that I like in social network.

  • AddThis plugin : This is an official plugin from AddThis team and it supports 305+ social networking services.

  • Easy Retweet : I added this plugin as well for retweeting your favorite post on twitter. Note that you will be able to see this in individual posts only, not in home page or list..

  • TweetMeme Retweet Button : This plugin is pretty cool. very stylish and well-written plugins. But I feel like Easy Retweet and TweetMem are a bit overlapped. I can’t decide which one will work best for you guys. Feel free to give me the feedback for this. I will see whether I should keep it both or remove either one based on your feedback.

  • Kouguu FB Like : If you are a facebook user like me, you might want to share your favorite thing with your facebook friends. That’s why I added “Kouguu FB Like” plugin in my blog so that you guys can simply click on “Like” button that you used to do whenever you feel like to share my articles with your friends.

  • Wickett Twitter Widget : Well, this is not really a sharing plugin but it will show you what I tweet on twitter. I hope this plugin will be useful when I can’t update my blog so often so my reader can read my twitter. By the way, my twitter id is @michaelsync in case you didn’t know..

I installed a few plugins like Fast and Secure Contact Form, WP Security Scan, WP-MalWatch and etc as well. That’s all for my blog.

Job

I’m sure that you guys already know that I left Xuenn last year. And then, I joined Consistel and worked for WPF projects for 7 months. After that, I recently joined Memolife as a Solution Architect to develop/refactor their Silverlight games. Don’t ask me why I moved one company to another so quickly. :) I will probably write about this in different post. Memolife is a interesting startup company and they are focusing on developing Silverlight games that can help users to increase the memory capacity and help them to memorize more things. We are currently refactoring our games and we will be using the following interesting things ~

  • Silverlight 4
  • Silverlight for Phone 7
  • Managed Extensibility Framework
  • Unity
  • Visual Studio Test Framework/Silverlight Test Framework
  • Moq
  • Enterprise Library or CLog (We haven’t decided it yet )
  • Scrum for managing project and some Xp practices

Yeah. Those are pretty cool stuffs. But of course, we are going to have a lot of challenges as well.

Education

I enrolled Master of Information Technology course in University of Southern Queensland a few months back. I’m currently taking two modules per semester since I couldn’t study full time. I need to complete 12 modules to get the master degree and I hope that I will be able to complete this course next year.

Okay.. That’s all from my side. Now, you guys tell me what you are doing lately.. :)