Thursday, July 14, 2011

Save Image/Picture from C# Object into Oracle BLOB

image

Some of my friends requested me to make a tutorial about Oracle database and C#. This time, I’d like to share about how to Save Image/Picture from C# Object into Oracle Blob Type. It’s very easy if you know the trick. In this tutorial, I don’t like to make this from zero, so, I’m using my framework (yeah, I made it Thumbs-up) in this tutorial. Okay, talk less do more.

1. First, create an empty table inside your database
CREATE TABLE GAMBAR 
(
  GAMBAR_BIN BLOB 
);

2. Next, download ImageViewer library here. What is it? It’s a WPF user control I made by myself to load external image into WPF window. So, just use it or you wish to modify it by yourself (It’s Open Source for you, but actually I hate Open Source Raised Eyebrow). After downloaded, extract it inside your disk.

3. Now, create a new WPF Application (I hate to write it Raised Eyebrow File –> New –> Project… –> WPF Application) and name your project WpfSaveToPhotoOracle. Oh yeah, I almost forgot, I’m using Visual Studio 2008 in this tutorial.

4. Add new reference to ImageViewer.dll that you have downloaded into your project.

image

5. Modify your window to become like this

image
or, if you really lazy, copy xaml code below:
<Window x:Class="WpfSavePhotoToOracle.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:ImageViewer;assembly=ImageViewer"
    Title="Window1" Height="324" Width="392" >
    <Grid>
        <s:ImageViewer Name="imgViewer" />
        <Button Height="23" HorizontalAlignment="Left" 
                Margin="106,0,0,31.4" Name="btnSaveToOracle" 
                VerticalAlignment="Bottom" Width="75" 
                Click="btnSaveToOracle_Click">Save</Button>
    </Grid>
</Window>

6. Next, the final step. Let’s get into the logic. Double click your Save button and modify the code like my code below:
using System.Windows;
using System.Data.OracleClient;
using System.Data;
using System;
 
namespace WpfSavePhotoToOracle
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        OracleConnection conn;
        OracleCommand cmd;
 
        public Window1()
        {
            InitializeComponent();
        }

        private void btnSaveToOracle_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                conn = new OracleConnection(
                    "Data Source=localhost;" +
                    "Persist Security Info=True;" +
                    "User ID=C07;Password=C07;Unicode=True");
                conn.Open();

                cmd = new OracleCommand(
                    "INSERT INTO GAMBAR VALUES(:blobtodb)",
                    conn);
                cmd.CommandType = CommandType.Text;
 
                OracleParameter param = cmd.Parameters.Add("blobtodb", 
                    OracleType.Blob);
                param.Direction = ParameterDirection.Input;
                param.Value = imgViewer.ImageBinaryData;

                cmd.ExecuteNonQuery();

                conn.Close();
                MessageBox.Show("Image saved");
            }
            catch (Exception ex)
            {
                MessageBox.Show("failed to save (" + ex.Message + ")");
            }
        }
    }
}

7. Now, test your project. Click Browse.., select a picture, and click save button. if you see a Message Box with “Image saved” string, then it’s succeeded.

image

8. Now, check your database (I’m using SQL developer) and you’ll see your image.

image

Okay, that’s all I got today. It’s time to get back to do my projects. See you on my next post Big Hug.

No comments:

Post a Comment

Please leave your comment here

Back to top