Original link
OLE Object data type
(1) OLE objects are used to create OLE objects in other programs using the OLE protocol, such as Microsoft Word documents,
Microsoft Excel spreadsheet, picture, sound, or other binary data. (2) The OLE object in Access corresponds to the data type in C # that is a binary byte array that corresponds to the oledbtype.binary type.
Use the same way as other types of fields, such as using a parameterized method:
OleDbParameter param = new OleDbParameter ("@Picture", oledbtype.binary);
(3) So, we can store images, sounds and other files in binary form into Access.
To Insert an OLE object: FileStream FileStream = new FileStream (filename, filemode.open); byte[] BFile = new byte[filestream.length];//allocates the array size fileStream. Read (bFile, 0, (int) fileStream. LENGTH);//Read the contents of the file into the array fileStream. Close ();//Closing file object
OleDbConnection conn = new OleDbConnection (CONNSTR);//Establish Connection Conn. Open ();//Opening connection
OleDbCommand COM = conn. CreateCommand ();
Where the picture field is an OLE Object data type Com.commandtext = "Insert into Test (ID, picture) Values (@id, @picture)"; Com. Parameters. AddWithValue ("@id", 0); Com. Parameters. AddWithValue ("@picture", bFile);
Com. ExecuteNonQuery ();
To read an OLE object: OleDbConnection conn = new OleDbConnection (CONNSTR); Conn. Open ();
OleDbCommand COM = conn. CreateCommand (); Com.commandtext = "Select Picture from Test where id=0";
Byte[] BFile = (byte[]) com. ExecuteScalar ();//convert to binary byte array after reading
Working with binary byte arrays
MemoryStream stream = new MemoryStream (bFile); Image img = Image.fromstream (stream);//restore binary byte array to original image
C # manipulating OLE objects for access [go]