Click here to Skip to main content
Click here to Skip to main content

Save Image/Picture from C# Object into Oracle BLOB

By , 21 Sep 2012
Rate this:
Please Sign up or sign in to vote.
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) 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). After downloaded, extract it inside your disk.

3. Now, create a new WPF Application (I hate to write it 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.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Junian Triajianto
Software Developer
Indonesia Indonesia
Just an ordinary geek.
 
For more information visit my blogs:
Junian Legacy
Kodefun, learning by coding
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 3 PinmemberKlaus Luedenscheidt21-Sep-12 17:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 22 Sep 2012
Article Copyright 2012 by Junian Triajianto
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid