This is a mirror of official site: http://jasper-net.blogspot.com/

Inserting images into MS Access file using OLEDB

| Tuesday, November 2, 2010
Design the form as above with a OpenFileDialog contol, 3 Labels, 3 TextBoxes, 11 buttons.

PictureBox1 Properties:

BorderStyle=Fixed3D; SizeMode=StrechImage

Note that OpenFileDialog control appears below the form(not on the form), which can be used for browsing an image.

Introduction:

Inorder to use OleDb Connection include the namespace: 'using System.Data.OleDb'

For accesing records from M.S.Access-2003 file we use 'Jet' driver,

And for accesing records from M.S.Access-2007 file we use 'Ace' driver.

As 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 data type 'ole object' for storing the image.

In this application, we will search a record by taking input from the InputBox. For this we have to add reference to Microsoft.VisualBasic.

Adding a Reference:

Goto Project Menu->Add Reference -> select 'Microsoft.VisualBasic' from .NET tab.

Inorder to use this we have to include the namespace: 'using Microsoft.VisualBasic'

Converting image into binary data:

We can't store an image directly into the database. For this we have two solutions:
To store the location of the image in the database
Converting the image into binary data and insert that binary data into database and convert that back to image while retrieving the records.
If we store the location of an image in the database, and suppose if that image is deleted or moved from that location, we will face problems while retrieving the records. So it is better to convert image into binary data and insert that binary data into database and convert that back to image while retrieving records.

We can convert an image into binary data using 1. FileStream (or) 2. MemoryStream

1. FileStream uses file location to convert an image into binary data which we may/may not provide during updation of a record.

Ex:

FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
byte[] photo_aray = new byte[fs.Length];
fs.Read(photo_aray, 0, photo_aray.Length);


Read more: C# Corner

Posted via email from .NET Info

0 comments: