简体   繁体   中英

Reading binary data from SQL Server CE database in C#

I have an image stored in my SQL Server CE database as binary data. The column is defined as an 'Image'. I would like to retrieve this data now back into an Image and display it in my picturebox control. I don't know exactly how to do this and I tried searching online, but there were no good articles explaining how to go about doing this.

So far I have the following code:

            if (dataReader.GetValue(3) != null)
            {
              // Retrieve binary data
              // create the image and add it to the picturebox.
            }

I'm assuming I have to use the following method:

dataReader.GetBytes()

I just don't know how where to start with this. Any comments would be helpful.

Assuming GetBytes() returns a byte array you can try something like this:

MemoryStream ms = new MemoryStream(dataReader.GetBytes());
Image img = Image.FromStream(ms);

// then assign img to your picture box

I've done this but not for a while, so you may have to do some of your own digging to complete this answer. Look at the static factory methods ("FromXYZ") of the Image class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM