DB생성
CREATE TABLE LobData
(
ID INTEGER NOT NULL ,
Image BLOB NOT NULL ,
FileName VARCHAR2(50) NULL ,
FileSize NUMBER NULL
);
CREATE UNIQUE INDEX XPKLobData ON LobData
(ID ASC);
ALTER TABLE LobData
ADD CONSTRAINT XPKLobData PRIMARY KEY (ID);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using System.IO;
namespace test_image
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.Columns[0].DataPropertyName = "FileName";
dataGridView1.Columns[1].DataPropertyName = "FileSize";
dataGridView1.Columns[2].DataPropertyName = "ID";
LoadImageData();
}
private void LoadImageData()
{
string SQL = "SELECT ID,FILENAME,FILESIZE FROM LobData";
OracleCommand cmd = GetCmd();
cmd.CommandText = SQL;
DataSet ds = new DataSet();
cmd.Connection.Open();
try
{
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cmd.Connection.Close();
cmd.Dispose();
}
dataGridView1.DataSource = ds.Tables[0];
}
private OracleCommand GetCmd()
{
string Connection = "Data Source=orcl;Persist Security Info=True;User ID=scott;Password=tiger;";
OracleConnection conn = new OracleConnection();
OracleCommand cmd = new OracleCommand();
conn.ConnectionString = Connection;
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
return cmd;
}
private void FileSave(byte[] file, string fileName, int fileSize)
{
// Command 생성
OracleCommand cmd = GetCmd();
string SQL = @"INSERT INTO LobData (ID,FILENAME,FILESIZE,Image) VALUES (SEQ_LOBDATA.NEXTVAL,:FILENAME,:FILESIZE,:Image)";
cmd.CommandText = SQL;
cmd.CommandType = CommandType.Text;
//파일명
cmd.Parameters.Add(":FILENAME", OracleDbType.Varchar2, 50).Value = fileName;
//파일사이즈
cmd.Parameters.Add(":FILESIZE", OracleDbType.Int32).Value = fileSize;
//이미지를 BLOB로
cmd.Parameters.Add(":Image", OracleDbType.Blob).Value = file;
cmd.Connection.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cmd.Connection.Close();
cmd.Dispose();
}
LoadImageData();
}
private void button1_Click(object sender, EventArgs e)
{
//선택된 파일을 Stream으로 받는다
Stream myStream;
/*************************
* 파일 다이알로그의 속성 설정
* ***********************/
// 기본 디렉토리 지정.
openFileDialog1.InitialDirectory = @"C\";
//선택 파일 종류 설정
openFileDialog1.Filter = "gif files (*.gif)|*.gif|jpg files (*.jpg)|*.jpg|bmp files (*.bmp)|*.bmp|All files (*.*)|*.*";
//jpg를 기본으로 선택
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
//파일 창을 연다.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//선택한 파일을 Stream으로 받는다.
if ((myStream = openFileDialog1.OpenFile()) != null)
{
//파일 사이즈 및 버퍼 사이즈
int iLen = (int)myStream.Length;
// 버퍼크기를 정한다.
byte[] myImage = new byte[iLen];
//저장할 파일 명을 가져온다.
string fileName = openFileDialog1.SafeFileName;
//파일을 byte로 읽는다
//이미지를 BLOB로 저장하기 위해서는 byte[]로 변경해야 한다.
myStream.Read(myImage, 0, iLen);
//파일을 닫는다.
myStream.Close();
//파일을 저장
FileSave(myImage, fileName, iLen);
}
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
ImageBind(e.RowIndex);
}
private void ImageBind(int Idx)
{
string strID = dataGridView1.Rows[Idx].Cells[2].Value.ToString();
string SQL = "SELECT Image FROM LOBDATA WHERE ID = " + strID;
OracleCommand cmd = GetCmd();
cmd.CommandText = SQL;
DataSet ds = new DataSet();
cmd.Connection.Open();
try
{
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cmd.Connection.Close();
cmd.Dispose();
}
DataRow dr = ds.Tables[0].Rows[0];
//버퍼 설정.
byte[] myByte = new byte[0];
//BLOB데이타를 byte[]로 변환
myByte = (byte[])dr["Image"];
//이미지 상자 크기에 마추어서 이미지 크기를 조절
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
//메모리를 백업저장소로 사용해서 이미지 스트림을 생성
MemoryStream ImgStream = new MemoryStream(myByte);
//스트림을 이미지로 변환해서 보여줌
pictureBox1.Image = Image.FromStream(ImgStream);
}
}
}