C # manipulating OLE objects for access [go]

Source: Internet
Author: User
Tags ole

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.