You are on page 1of 7

Inserting images into m.s.access file using oledb in c#.

net
Objective:
To insert images into m.s.access file and also to perform search, update and delete operations.
Design:
Design the form as above with a OpenFileDialog contol,
3 Labels, 3 TextBoxes, 11 buttons.
ictureBox1 roperties!
Bor"er#t$le%Fixe"3D& #i'e(o"e%#trech)mage
*ote that OpenFileDialog control appears below the form+not on the form,, which
can be use" for browsing an image.
Introduction!
)nor"er to use OleDb -onnection inclu"e the namespace!
.using #$stem.Data.OleDb/
For accesing recor"s from (.#.0ccess12333 file we use .4et/ "river,
0n" for accesing recor"s from (.#.0ccess12335 file we use .0ce/ "river.
0s we want to insert images into the msaccess, first we have to create a table
in the m.s.access file, we can use the "ata t$pe .ole ob6ect/ for storing the
image.
->)n this application, we will search a recor" b$ ta7ing input from the
)nputBox. For this we have to a"" reference to (icrosoft.8isualBasic.
Adding a Reference:
9oto ro6ect (enu
1:0"" ;eference 1: select <(icrosoft.8isualBasic< from .*=T tab.
)nor"er to use this we have to inclu"e the namespace!
.using (icrosoft.8isualBasic/
*Converting image into binary data:
>e can/t store an image "irectl$ into the "atabase. For this we have two
solutions!
i, To store the location of the image in the "atabase
ii, -onverting the image into binar$ "ata an" insert that binar$ "ata into
"atabase an" convert that bac7 to image while retrieving the recor"s.
)f we store the location of an image in the "atabase, an" suppose if that image
is "elete" or move" from that location, we will face problems while retrieving
the recor"s. #o it is better to convert image into binar$ "ata an" insert that
binar$ "ata into "atabase an" convert that bac7 to image while retrieving
recor"s.
1:>e can convert an image into binar$ "ata using 1. FileStream +or, 2.
emoryStream
1. File#tream uses file location to convert an image into binar$ "ata which we
ma$?ma$ not provi"e "uring up"ation of a recor".
!":
File#tream fs % new File#tream+openFileDialog1.File*ame, File(o"e.Open,
File0ccess.;ea",&
b$te@A photoBara$ % new b$te@fs.LengthA&
fs.;ea"+photoBara$, 3, photoBara$.Length,&
2. #o it is better to use emoryStream which uses image in the ictureBox to
convert an image into binar$ "ata.
!":
(emor$#tream ms % new (emor$#tream+,&
pictureBox1.)mage.#ave+ms, )mageFormat.4peg,&
b$te@A photoBara$ % new b$te@ms.LengthA&
ms.osition % 3&
ms.;ea"+photoBara$, 3, photoBara$.Length,&
1:)nor"er to use File#tream or (emor$#tream we have to inclu"e the namespace!
.using #$stem.)O/.

#$enFile%ialog Control:
>e use OpenFileDialog control inor"er to browse for the images +photos, to
insert into the recor".
->creating a $rimary &ey in t'e data(able:
)n this app. we use Fin"+, metho" to search a recor", which reCuires
"etails of primar$7e$ column for "atabase tables this provi"e" using statement!
a"apter.(issing#chema0ction % (issing#chema0ction.0"">ithDe$
But as we "on/t have an$ primar$7e$ column in (.#.0ccess table, we have to
create a primar$ 7e$ column in "atatable.
=g!
"s.Tables@3A.-onstraints.0""+Ep7BsnoE, "s.Tables@3A.-olumns@3A, true)*
1111111111111
Code:
using #$stem&
using #$stem.Data&
using #$stem.Drawing&
using #$stem.Drawing.)maging&
using #$stem.Data.OleDb&
using #$stem.>in"ows.Forms&
using #$stem.)O&
using (icrosoft.8isualBasic&
namespace accessBimg
F
public partial class Form1 ! Form
F
public Form1+,
F
)nitiali'e-omponent+,&
G
OleDb-onnection con&
OleDb-omman" cm"&
OleDbData0"apter a"apter&
Data#et "s&
int rno%3&
(emor$#tream ms&
b$te@A photoBara$&
private voi" Form1BLoa"+ob6ect sen"er, =vent0rgs e,
F
con % new OleDb-onnection+HE provi"er%microsoft.ace.ole"b.12.3& "ata
source%e!IprashIstu".acc"b& persist securit$i)nfo%falseE,&??stu".acc"b1:access35
filename
??con % new OleDb-onnection+HE provi"er%microsoft.6et.ole"b.J.3&
"ata source%e!IprashIstu".m"bE,&??stu".m"b1:access33 filename
loa""ata+,&
show"ata+,&
G
voi" loa""ata+,
F
a"apter % new OleDbData0"apter+Eselect K from stu"entE, con,&
"s % new Data#et+,&??stu"ent1: table name in stu".acc"b?stu".m"b file
a"apter.Fill+"s, Estu"entE,&
"s.Tables@3A.-onstraints.0""+Ep7BsnoE, "s.Tables@3A.-olumns@3A,
true,&??creating primar$ 7e$ for Tables@3A in "ataset
G
voi" show"ata+,
F
textBox1.Text % "s.Tables@3A.;ows@rnoA@3A.To#tring+,&
textBox2.Text % "s.Tables@3A.;ows@rnoA@1A.To#tring+,&
textBox3.Text % "s.Tables@3A.;ows@rnoA@2A.To#tring+,&
pictureBox1.)mage % null&
if +"s.Tables@3A.;ows@rnoA@3A L% #$stem.DB*ull.8alue,
F
photoBara$ % +b$te@A,"s.Tables@3A.;ows@rnoA@3A&
(emor$#tream ms % new (emor$#tream+photoBara$,&
pictureBox1.)mage % )mage.From#tream+ms,&
G
G
private voi" btnBrowseB-lic7+ob6ect sen"er, =vent0rgs e,
F
openFileDialog1.Filter % E6pegMK.6pgMbmpMK.bmpMall filesMK.KE&
Dialog;esult res % openFileDialog1.#howDialog+,&
if +res %% Dialog;esult.OD,
F
pictureBox1.)mage % )mage.FromFile+openFileDialog1.File*ame,&
G
G
private voi" btn)nsertB-lic7+ob6ect sen"er, =vent0rgs e,
F
cm" % new OleDb-omman"+Einsert into stu"ent+sno,sname,course,photo,
values+E N textBox1.Text N E,<E N textBox2.Text N E<,<E N textBox3.Text N
E<,Hphoto,E, con,&
convBphoto+,&
con.Open+,&
int n % cm".=xecute*onOuer$+,&
con.-lose+,&
if +n : 3,
F
(essageBox.#how+Erecor" inserte"E,&
loa""ata+,&
rnoNN&
G
else
(essageBox.#how+Einsertion faile"E,&
G
voi" convBphoto+,
F
??converting photo to binar$ "ata
if +pictureBox1.)mage L% null,
F
??using File#tream!+will not wor7 while up"ating, if image is
not change",
??File#tream fs % new File#tream+openFileDialog1.File*ame,
File(o"e.Open, File0ccess.;ea",&
??b$te@A photoBara$ % new b$te@fs.LengthA&
??fs.;ea"+photoBara$, 3, photoBara$.Length,&
??using (emor$#tream!
ms % new (emor$#tream+,&
pictureBox1.)mage.#ave+ms, )mageFormat.4peg,&
b$te@A photoBara$ % new b$te@ms.LengthA&
ms.osition % 3&
ms.;ea"+photoBara$, 3, photoBara$.Length,&
cm".arameters.0"">ith8alue+EHphotoE, photoBara$,&
G
G
private voi" btn#earchB-lic7+ob6ect sen"er, =vent0rgs e,
F
tr$
F
int n % -onvert.To)nt32+)nteraction.)nputBox+E=nter sno!E,
E#earchE, E23E, 133, 133,,&
Data;ow "row&
"row % "s.Tables@3A.;ows.Fin"+n,&
if +"row L% null,
F
rno % "s.Tables@3A.;ows.)n"exOf+"row,&
textBox1.Text % "row@3A.To#tring+,&
textBox2.Text % "row@1A.To#tring+,&
textBox3.Text % "row@2A.To#tring+,&
pictureBox1.)mage % null&
if +"row@3A L% #$stem.DB*ull.8alue,
F
photoBara$ % +b$te@A,"row@3A&
(emor$#tream ms % new (emor$#tream+photoBara$,&
pictureBox1.)mage % )mage.From#tream+ms,&
G
G
else
(essageBox.#how+E;ecor" *ot Foun"E,&
G
catch
F
(essageBox.#how+E)nvali" )nputE,&
G
G
private voi" btnPp"ateB-lic7+ob6ect sen"er, =vent0rgs e,
F
cm" % new OleDb-omman"+Eup"ate stu"ent set sname%<E N textBox2.Text
N E<, course%<E N textBox3.Text N E<, photo%Hphoto where sno%E N textBox1.Text,
con,&
convBphoto+,&
con.Open+,&
int n % cm".=xecute*onOuer$+,&
con.-lose+,&
if +n : 3,
F
(essageBox.#how+E;ecor" Pp"ate"E,&
loa""ata+,&
G
else
(essageBox.#how+EPp"ation Faile"E,&
G
private voi" btnDeleteB-lic7+ob6ect sen"er, =vent0rgs e,
F
cm" % new OleDb-omman"+E"elete from stu"ent where sno%E N
textBox1.Text, con,&
con.Open+,&
int n % cm".=xecute*onOuer$+,&
con.-lose+,&
if +n : 3,
F
(essageBox.#how+E;ecor" Delete"E,&
loa""ata+,&
rno % 3&
show"ata+,&
G
else
(essageBox.#how+EDeletion faile"E,&
G
private voi" btnFirstB-lic7+ob6ect sen"er, =vent0rgs e,
F
if +"s.Tables@3A.;ows.-ount : 3,
F
rno % 3&
show"ata+,&
(essageBox.#how+EFirst ;ecor"E,&
G
else
(essageBox.#how+Eno recor"sE,&
G
private voi" btnreviousB-lic7+ob6ect sen"er, =vent0rgs e,
F
if +"s.Tables@3A.;ows.-ount : 3,
F
if +rno : 3,
F
rno11&
show"ata+,&
G
else
(essageBox.#how+EFirst ;ecor"E,&
G
else
(essageBox.#how+Eno recor"sE,&
G
private voi" btn*extB-lic7+ob6ect sen"er, =vent0rgs e,
F
if +"s.Tables@3A.;ows.-ount : 3,
F
if +rno Q "s.Tables@3A.;ows.-ount 1 1,
F
rnoNN&
show"ata+,&
G
else
(essageBox.#how+ELast ;ecor"E,&
G
else
(essageBox.#how+Eno recor"sE,&
G
private voi" btnLastB-lic7+ob6ect sen"er, =vent0rgs e,
F
if +"s.Tables@3A.;ows.-ount : 3,
F
rno % "s.Tables@3A.;ows.-ount 1 1&
show"ata+,&
(essageBox.#how+ELast ;ecor"E,&
G
else
(essageBox.#how+Eno recor"sE,&
G
private voi" btn-learB-lic7+ob6ect sen"er, =vent0rgs e,
F
textBox1.Text % textBox2.Text % textBox3.Text % EE&
G
private voi" btn=xitB-lic7+ob6ect sen"er, =vent0rgs e,
F
this.-lose+,&
G

G
G
+ote:
.stu".acc"b/ an" .stu".m"b/ files are provi"e" in accessBimg.'ip file along with
source co"e

You might also like